fixing issues that broke tests

This commit is contained in:
Rob Rodriguez 2018-12-10 17:43:50 -08:00
parent 25674faa7d
commit 2cc2a1555d
4 changed files with 23 additions and 24 deletions

View File

@ -75,8 +75,7 @@ func updateCallback(scope *Scope) {
} else {
for _, field := range scope.Fields() {
if scope.changeableField(field) {
if !field.IsPrimaryKey {
if field.IsNormal {
if !field.IsPrimaryKey && field.IsNormal {
if !field.IsForeignKey || !field.IsBlank || !field.HasDefaultValue {
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface())))
}
@ -88,7 +87,6 @@ func updateCallback(scope *Scope) {
scope.Err(err)
}
}
}
} else if relationship := field.Relationship; relationship != nil && relationship.Kind == "belongs_to" {
for _, foreignKey := range relationship.ForeignDBNames {
if foreignField, ok := scope.FieldByName(foreignKey); ok && !scope.changeableField(foreignField) {

View File

@ -4,6 +4,8 @@ import (
"encoding/json"
"errors"
"testing"
"github.com/kr/pretty"
)
type (
@ -78,8 +80,6 @@ func (m *WidgetUser) DecodeField(column string, value interface{}) error {
}
func TestEncoder(t *testing.T) {
DB.AutoMigrate(&WidgetUser{})
user := &WidgetUser{
User: User{
Id: 1,
@ -110,4 +110,5 @@ func TestEncoder(t *testing.T) {
t.Errorf("user widget is not valid")
}
}
pretty.Log(user1.Widget)
}

View File

@ -292,7 +292,7 @@ func runMigration() {
DB.Exec(fmt.Sprintf("drop table %v;", table))
}
values := []interface{}{&Short{}, &ReallyLongThingThatReferencesShort{}, &ReallyLongTableNameToTestMySQLNameLengthLimit{}, &NotSoLongTableName{}, &Product{}, &Email{}, &Address{}, &CreditCard{}, &Company{}, &Role{}, &Language{}, &HNPost{}, &EngadgetPost{}, &Animal{}, &User{}, &JoinTable{}, &Post{}, &Category{}, &Comment{}, &Cat{}, &Dog{}, &Hamster{}, &Toy{}, &ElementWithIgnoredField{}, &Place{}}
values := []interface{}{&Short{}, &ReallyLongThingThatReferencesShort{}, &ReallyLongTableNameToTestMySQLNameLengthLimit{}, &NotSoLongTableName{}, &Product{}, &Email{}, &Address{}, &CreditCard{}, &Company{}, &Role{}, &Language{}, &HNPost{}, &EngadgetPost{}, &Animal{}, &User{}, &JoinTable{}, &Post{}, &Category{}, &Comment{}, &Cat{}, &Dog{}, &Hamster{}, &Toy{}, &ElementWithIgnoredField{}, &Place{}, &WidgetUser{}}
for _, value := range values {
DB.DropTable(value)
}

View File

@ -606,6 +606,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
}
}
}
}
// Even it is ignored, also possible to decode db value into the field
if value, ok := field.TagSettingsGet("COLUMN"); ok {
@ -616,7 +617,6 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
modelStruct.StructFields = append(modelStruct.StructFields, field)
}
}
if len(modelStruct.PrimaryFields) == 0 {
if field := getForeignField("id", modelStruct.StructFields); field != nil {