Use the sql default value (of the tag field) when updating a field to a blank value

This commit is contained in:
Paolo Galeone 2015-11-22 21:48:32 +01:00
parent 86afba0dc5
commit 40b51709ff
3 changed files with 26 additions and 5 deletions

View File

@ -51,10 +51,16 @@ 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 {
if !field.HasDefaultValue || !field.IsBlank { if field.HasDefaultValue {
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface()))) if field.IsBlank {
} else if field.HasDefaultValue { defaultValue := strings.Trim(parseTagSetting(field.Tag.Get("sql"))["DEFAULT"], "'")
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(defaultValue)))
} else {
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface())))
}
scope.InstanceSet("gorm:force_reload", true) scope.InstanceSet("gorm:force_reload", true)
} else {
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" {
for _, dbName := range relationship.ForeignDBNames { for _, dbName := range relationship.ForeignDBNames {

View File

@ -134,6 +134,7 @@ type Animal struct {
unexported string // unexported value unexported string // unexported value
CreatedAt time.Time CreatedAt time.Time
UpdatedAt time.Time UpdatedAt time.Time
Cool bool `sql:"default:false"`
} }
type JoinTable struct { type JoinTable struct {

View File

@ -87,10 +87,11 @@ func TestUpdateWithNoStdPrimaryKeyAndDefaultValues(t *testing.T) {
DB.Save(&animal) DB.Save(&animal)
updatedAt1 := animal.UpdatedAt updatedAt1 := animal.UpdatedAt
// Sleep for a second and than update a field
time.Sleep(1000 * time.Millisecond)
DB.Save(&animal).Update("name", "Francis") DB.Save(&animal).Update("name", "Francis")
if updatedAt1.Format(time.RFC3339Nano) == animal.UpdatedAt.Format(time.RFC3339Nano) { if updatedAt1.Format(time.RFC3339Nano) == animal.UpdatedAt.Format(time.RFC3339Nano) {
t.Errorf("updatedAt should not be updated if nothing changed") t.Errorf("updatedAt should be updated when changing a field")
} }
var animals []Animal var animals []Animal
@ -118,6 +119,19 @@ func TestUpdateWithNoStdPrimaryKeyAndDefaultValues(t *testing.T) {
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 with an associated default value should not occur when trying to insert an empty field. The default one should be inserted\n")
} }
// Animal.Cool has a default value thats equal to the Zero of its type. (false) I have to update this field to true and false without problems
animal.Cool = true
DB.Save(&animal)
if !animal.Cool {
t.Errorf("I should update a field with a default value to someother value")
}
animal.Cool = false
DB.Save(&animal)
if animal.Cool {
t.Errorf("I should update a field with an associated blank value to its blank value")
}
} }
func TestUpdates(t *testing.T) { func TestUpdates(t *testing.T) {