Fix Blank Associated Values
This commit is contained in:
parent
81c17a7e25
commit
474edd63a7
@ -76,7 +76,7 @@ func updateCallback(scope *Scope) {
|
||||
for _, field := range scope.Fields() {
|
||||
if scope.changeableField(field) {
|
||||
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())))
|
||||
}
|
||||
} else if relationship := field.Relationship; relationship != nil && relationship.Kind == "belongs_to" {
|
||||
|
@ -98,12 +98,12 @@ func TestRunCallbacks(t *testing.T) {
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
|
||||
|
@ -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{}, &NewPerson{}, &NewAddress{}}
|
||||
for _, value := range values {
|
||||
DB.DropTable(value)
|
||||
}
|
||||
|
@ -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 {
|
||||
Id int64
|
||||
Value string
|
||||
|
Loading…
x
Reference in New Issue
Block a user