Fix Blank Associated Values

This commit is contained in:
Jacques Racagneres 2019-09-26 01:42:26 +01:00
parent 81c17a7e25
commit 474edd63a7
No known key found for this signature in database
GPG Key ID: 284DB65BF94294AF
4 changed files with 58 additions and 4 deletions

View File

@ -76,7 +76,7 @@ func updateCallback(scope *Scope) {
for _, field := range scope.Fields() { for _, field := range scope.Fields() {
if scope.changeableField(field) { if scope.changeableField(field) {
if !field.IsPrimaryKey && field.IsNormal && (field.Name != "CreatedAt" || !field.IsBlank) { if !field.IsPrimaryKey && field.IsNormal && (field.Name != "CreatedAt" || !field.IsBlank) {
if !field.IsForeignKey || !field.IsBlank || !field.HasDefaultValue { if !field.IsBlank || field.HasDefaultValue {
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface()))) sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface())))
} }
} else if relationship := field.Relationship; relationship != nil && relationship.Kind == "belongs_to" { } else if relationship := field.Relationship; relationship != nil && relationship.Kind == "belongs_to" {

View File

@ -98,12 +98,12 @@ func TestRunCallbacks(t *testing.T) {
} }
DB.Where("Code = ?", "unique_code").First(&p) DB.Where("Code = ?", "unique_code").First(&p)
if !reflect.DeepEqual(p.GetCallTimes(), []int64{1, 2, 1, 1, 0, 0, 0, 0, 2}) { if !reflect.DeepEqual(p.GetCallTimes(), []int64{1, 2, 0, 1, 0, 0, 0, 0, 2}) {
t.Errorf("After update callbacks values are not saved, %v", p.GetCallTimes()) t.Errorf("After update callbacks values are not saved, %v", p.GetCallTimes())
} }
DB.Delete(&p) DB.Delete(&p)
if !reflect.DeepEqual(p.GetCallTimes(), []int64{1, 2, 1, 1, 0, 0, 1, 1, 2}) { if !reflect.DeepEqual(p.GetCallTimes(), []int64{1, 2, 0, 1, 0, 0, 1, 1, 2}) {
t.Errorf("After delete callbacks should be invoked successfully, %v", p.GetCallTimes()) t.Errorf("After delete callbacks should be invoked successfully, %v", p.GetCallTimes())
} }

View File

@ -292,7 +292,7 @@ func runMigration() {
DB.Exec(fmt.Sprintf("drop table %v;", table)) 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{}, &NewPerson{}, &NewAddress{}}
for _, value := range values { for _, value := range values {
DB.DropTable(value) DB.DropTable(value)
} }

View File

@ -419,6 +419,60 @@ func TestUpdatesWithBlankValues(t *testing.T) {
} }
} }
type NewAddress struct {
ID int
HouseNumber int
LineOne string
}
type NewPerson struct {
ID int
Name string
NewAddress NewAddress
NewAddressID int
}
func TestAssociatedBlankValues(t *testing.T) {
DB.LogMode(true)
person1 := NewPerson{
ID: 1,
Name: "Person",
NewAddress: NewAddress{
ID: 1,
HouseNumber: 1,
LineOne: "Street One",
},
}
DB.Save(&person1)
person1Update := NewPerson{
ID: 1,
NewAddress: NewAddress{
ID: 1,
HouseNumber: 2,
},
}
DB.Model(&person1Update).Update(person1Update)
var personGet NewPerson
DB.Preload("NewAddress").Find(&personGet)
if personGet.NewAddress.LineOne != "Street One" {
t.Errorf("line one should be 'Street One' as it was updated with a blank value. Value is %s", personGet.NewAddress.LineOne)
}
if personGet.NewAddress.HouseNumber != 2 {
t.Errorf("house number should be 2 following the update. Value is %d", personGet.NewAddress.HouseNumber)
}
if personGet.Name != "Person" {
t.Errorf("name should be 'Person' as it was updated with a blank value. Value is %s", personGet.Name)
}
}
type ElementWithIgnoredField struct { type ElementWithIgnoredField struct {
Id int64 Id int64
Value string Value string