Fix update of fields with a default value
This commit is contained in:
parent
2f9abdcf02
commit
0923458e52
@ -197,4 +197,10 @@ func (c *callback) sort() {
|
|||||||
c.rowQueries = sortProcessors(rowQueries)
|
c.rowQueries = sortProcessors(rowQueries)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ForceReload(scope *Scope) {
|
||||||
|
if _, ok := scope.InstanceGet("gorm:force_reload"); ok {
|
||||||
|
scope.DB().New().First(scope.Value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var DefaultCallback = &callback{processors: []*callbackProcessor{}}
|
var DefaultCallback = &callback{processors: []*callbackProcessor{}}
|
||||||
|
@ -33,7 +33,7 @@ func Create(scope *Scope) {
|
|||||||
columns = append(columns, scope.Quote(field.DBName))
|
columns = append(columns, scope.Quote(field.DBName))
|
||||||
sqls = append(sqls, scope.AddToVars(field.Field.Interface()))
|
sqls = append(sqls, scope.AddToVars(field.Field.Interface()))
|
||||||
} else if field.HasDefaultValue {
|
} else if field.HasDefaultValue {
|
||||||
scope.InstanceSet("gorm:force_reload_after_create", true)
|
scope.InstanceSet("gorm:force_reload", true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if relationship := field.Relationship; relationship != nil && relationship.Kind == "belongs_to" {
|
} else if relationship := field.Relationship; relationship != nil && relationship.Kind == "belongs_to" {
|
||||||
@ -97,12 +97,6 @@ func Create(scope *Scope) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ForceReloadAfterCreate(scope *Scope) {
|
|
||||||
if _, ok := scope.InstanceGet("gorm:force_reload_after_create"); ok {
|
|
||||||
scope.DB().New().First(scope.Value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func AfterCreate(scope *Scope) {
|
func AfterCreate(scope *Scope) {
|
||||||
scope.CallMethodWithErrorCheck("AfterCreate")
|
scope.CallMethodWithErrorCheck("AfterCreate")
|
||||||
scope.CallMethodWithErrorCheck("AfterSave")
|
scope.CallMethodWithErrorCheck("AfterSave")
|
||||||
@ -114,7 +108,7 @@ func init() {
|
|||||||
DefaultCallback.Create().Register("gorm:save_before_associations", SaveBeforeAssociations)
|
DefaultCallback.Create().Register("gorm:save_before_associations", SaveBeforeAssociations)
|
||||||
DefaultCallback.Create().Register("gorm:update_time_stamp_when_create", UpdateTimeStampWhenCreate)
|
DefaultCallback.Create().Register("gorm:update_time_stamp_when_create", UpdateTimeStampWhenCreate)
|
||||||
DefaultCallback.Create().Register("gorm:create", Create)
|
DefaultCallback.Create().Register("gorm:create", Create)
|
||||||
DefaultCallback.Create().Register("gorm:force_reload_after_create", ForceReloadAfterCreate)
|
DefaultCallback.Create().Register("gorm:force_reload", ForceReload)
|
||||||
DefaultCallback.Create().Register("gorm:save_after_associations", SaveAfterAssociations)
|
DefaultCallback.Create().Register("gorm:save_after_associations", SaveAfterAssociations)
|
||||||
DefaultCallback.Create().Register("gorm:after_create", AfterCreate)
|
DefaultCallback.Create().Register("gorm:after_create", AfterCreate)
|
||||||
DefaultCallback.Create().Register("gorm:commit_or_rollback_transaction", CommitOrRollbackTransaction)
|
DefaultCallback.Create().Register("gorm:commit_or_rollback_transaction", CommitOrRollbackTransaction)
|
||||||
|
@ -43,7 +43,7 @@ func Update(scope *Scope) {
|
|||||||
|
|
||||||
if updateAttrs, ok := scope.InstanceGet("gorm:update_attrs"); ok {
|
if updateAttrs, ok := scope.InstanceGet("gorm:update_attrs"); ok {
|
||||||
for key, value := range updateAttrs.(map[string]interface{}) {
|
for key, value := range updateAttrs.(map[string]interface{}) {
|
||||||
if scope.changeableDBColumn(key) {
|
if scope.isChangeableDBColumn(key) {
|
||||||
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(key), scope.AddToVars(value)))
|
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(key), scope.AddToVars(value)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -51,7 +51,11 @@ func Update(scope *Scope) {
|
|||||||
fields := scope.Fields()
|
fields := scope.Fields()
|
||||||
for _, field := range fields {
|
for _, field := range fields {
|
||||||
if scope.changeableField(field) && !field.IsPrimaryKey && field.IsNormal {
|
if scope.changeableField(field) && !field.IsPrimaryKey && field.IsNormal {
|
||||||
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface())))
|
if !field.HasDefaultValue || !field.IsBlank {
|
||||||
|
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface())))
|
||||||
|
} else if field.HasDefaultValue {
|
||||||
|
scope.InstanceSet("gorm:force_reload", true)
|
||||||
|
}
|
||||||
} else if relationship := field.Relationship; relationship != nil && relationship.Kind == "belongs_to" {
|
} else if relationship := field.Relationship; relationship != nil && relationship.Kind == "belongs_to" {
|
||||||
for _, dbName := range relationship.ForeignDBNames {
|
for _, dbName := range relationship.ForeignDBNames {
|
||||||
if relationField := fields[dbName]; !scope.changeableField(relationField) && !relationField.IsBlank {
|
if relationField := fields[dbName]; !scope.changeableField(relationField) && !relationField.IsBlank {
|
||||||
@ -92,4 +96,5 @@ func init() {
|
|||||||
DefaultCallback.Update().Register("gorm:save_after_associations", SaveAfterAssociations)
|
DefaultCallback.Update().Register("gorm:save_after_associations", SaveAfterAssociations)
|
||||||
DefaultCallback.Update().Register("gorm:after_update", AfterUpdate)
|
DefaultCallback.Update().Register("gorm:after_update", AfterUpdate)
|
||||||
DefaultCallback.Update().Register("gorm:commit_or_rollback_transaction", CommitOrRollbackTransaction)
|
DefaultCallback.Update().Register("gorm:commit_or_rollback_transaction", CommitOrRollbackTransaction)
|
||||||
|
DefaultCallback.Update().Register("gorm:force_reload", ForceReload)
|
||||||
}
|
}
|
||||||
|
2
scope.go
2
scope.go
@ -411,7 +411,7 @@ func (scope *Scope) OmitAttrs() []string {
|
|||||||
return scope.Search.omits
|
return scope.Search.omits
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) changeableDBColumn(column string) bool {
|
func (scope *Scope) isChangeableDBColumn(column string) bool {
|
||||||
selectAttrs := scope.SelectAttrs()
|
selectAttrs := scope.SelectAttrs()
|
||||||
omitAttrs := scope.OmitAttrs()
|
omitAttrs := scope.OmitAttrs()
|
||||||
|
|
||||||
|
@ -101,7 +101,6 @@ func TestUpdateWithNoStdPrimaryKeyAndDefaultValues(t *testing.T) {
|
|||||||
|
|
||||||
animal = Animal{From: "somewhere"} // No name fields, should be filled with the default value (galeone)
|
animal = Animal{From: "somewhere"} // No name fields, should be filled with the default value (galeone)
|
||||||
DB.Save(&animal).Update("From", "a nice place") // The name field shoul be untouched
|
DB.Save(&animal).Update("From", "a nice place") // The name field shoul be untouched
|
||||||
DB.First(&animal, animal.Counter)
|
|
||||||
if animal.Name != "galeone" {
|
if animal.Name != "galeone" {
|
||||||
t.Errorf("Name fiels shouldn't be changed if untouched, but got %v", animal.Name)
|
t.Errorf("Name fiels shouldn't be changed if untouched, but got %v", animal.Name)
|
||||||
}
|
}
|
||||||
@ -109,17 +108,15 @@ func TestUpdateWithNoStdPrimaryKeyAndDefaultValues(t *testing.T) {
|
|||||||
// When changing a field with a default value, the change must occur
|
// When changing a field with a default value, the change must occur
|
||||||
animal.Name = "amazing horse"
|
animal.Name = "amazing horse"
|
||||||
DB.Save(&animal)
|
DB.Save(&animal)
|
||||||
DB.First(&animal, animal.Counter)
|
|
||||||
if animal.Name != "amazing horse" {
|
if animal.Name != "amazing horse" {
|
||||||
t.Errorf("Update a filed with a default value should occur. But got %v\n", animal.Name)
|
t.Errorf("Update a filed with a default value should occur. But got %v\n", animal.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// When changing a field with a default value with blank value
|
// When changing a field with a default value with blank value, the DBMS should insert the default value. Not the empty one.
|
||||||
animal.Name = ""
|
animal.Name = ""
|
||||||
DB.Save(&animal)
|
DB.Save(&animal)
|
||||||
DB.First(&animal, animal.Counter)
|
if animal.Name == "" {
|
||||||
if animal.Name != "" {
|
t.Errorf("Update a filed with an associated default value should not occur when trying to insert an empty field. The default one should be inserted\n")
|
||||||
t.Errorf("Update a filed to blank with a default value should occur. But got %v\n", animal.Name)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user