From 75819b2bb320ae039297e4bfee2c9bdfd019a7bd Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Mon, 17 Nov 2014 17:38:32 +0800 Subject: [PATCH] Refactor DefaultValue --- README.md | 32 +++++--------------------------- callback_create.go | 3 +-- callback_update.go | 3 +-- structs_test.go | 2 +- 4 files changed, 8 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 6dbe98f4..dba65d5f 100644 --- a/README.md +++ b/README.md @@ -974,8 +974,8 @@ If you have an existing database schema, and the primary key field is different ```go type Animal struct { AnimalId int64 `gorm:"primary_key:yes"` - Birthday time.Time `sql:"DEFAULT:NOW()"` - Name string `sql:"default:'galeone'"` + Birthday time.Time `sql:"DEFAULT:current_timestamp"` + Name string `sql:"default:'galeone'"` Age int64 } ``` @@ -992,44 +992,22 @@ type Animal struct { ## Default values -If you have defined a default value in the `sql` tag (see the struct Animal above) the generated queries will not contains the value for these fields if is not set. +If you have defined a default value in the `sql` tag (see the struct Animal above) the generated create/update SQl will ignore these fields if is set blank data. Eg. ```go -db.Save(&Animal{Age: 99}) +db.Create(&Animal{Age: 99, Name: ""}) ``` The generated query will be: ```sql -INSERT INTO animals("birthday","age","name") values(NOW(), '99', 'galeone') +INSERT INTO animals("age") values('99'); ``` The same thing occurs in update statements. -You should fetch the value again to get the default updated values in the struct. - -We can't do the same thing of the primary key (that is always filled with the right value) because default SQL values can be expressions and thus be different from the fields' type (eg. a time.Time fiels has a default value of "NOW()") - -So the right way to do an update/insert statement and be sure to get the default values in the struct is - -```go -//Insert -var animal Animal -animal.Age = 99 -db.Save(&animal) -db.First(&animal, animal.AnimalId) -// Now wo have the animal struct with: -// Birthday: the insert time -// Name: the string galeone -// Age: the setted one -> 99 - -// For the update statements is the same -// First save the struct -// Than fetch it back again -``` - ## More examples with query chain ```go diff --git a/callback_create.go b/callback_create.go index 73d1088d..1510122f 100644 --- a/callback_create.go +++ b/callback_create.go @@ -2,7 +2,6 @@ package gorm import ( "fmt" - "reflect" "strings" ) @@ -27,7 +26,7 @@ func Create(scope *Scope) { var sqls, columns []string for _, field := range scope.Fields() { if field.IsNormal && (!field.IsPrimaryKey || !scope.PrimaryKeyZero()) { - if field.DefaultValue != nil && reflect.DeepEqual(field.Field.Interface(), reflect.Zero(field.Field.Type()).Interface()) { + if field.DefaultValue != nil && field.IsBlank { continue } columns = append(columns, scope.Quote(field.DBName)) diff --git a/callback_update.go b/callback_update.go index 04fa516a..0e32352f 100644 --- a/callback_update.go +++ b/callback_update.go @@ -2,7 +2,6 @@ package gorm import ( "fmt" - "reflect" "strings" ) @@ -50,7 +49,7 @@ func Update(scope *Scope) { } else { for _, field := range scope.Fields() { if !field.IsPrimaryKey && field.IsNormal && !field.IsIgnored { - if field.DefaultValue != nil && reflect.DeepEqual(field.Field.Interface(), reflect.Zero(field.Field.Type()).Interface()) { + if field.DefaultValue != nil && field.IsBlank { continue } sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface()))) diff --git a/structs_test.go b/structs_test.go index 750cc867..9b2dd5a8 100644 --- a/structs_test.go +++ b/structs_test.go @@ -128,7 +128,7 @@ type Animal struct { Counter uint64 `gorm:"primary_key:yes"` Name string `sql:"DEFAULT:'galeone'"` From string //test reserved sql keyword as field name - Age time.Time `sql:"DEFAULT:NOW()"` + Age time.Time `sql:"DEFAULT:current_timestamp"` CreatedAt time.Time UpdatedAt time.Time }