forked from mirror/gorm
Refactor DefaultValue
This commit is contained in:
parent
064d91335b
commit
75819b2bb3
32
README.md
32
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
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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())))
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue