mirror of https://github.com/go-gorm/gorm.git
Don't query all columns out from database after create, but only those has default value
This commit is contained in:
parent
84c6b46011
commit
4c1a78bab7
|
@ -33,7 +33,12 @@ 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)
|
var hasDefaultValueColumns []string
|
||||||
|
if oldHasDefaultValueColumns, ok := scope.InstanceGet("gorm:force_reload_after_create_attrs"); ok {
|
||||||
|
hasDefaultValueColumns = oldHasDefaultValueColumns.([]string)
|
||||||
|
}
|
||||||
|
hasDefaultValueColumns = append(hasDefaultValueColumns, field.DBName)
|
||||||
|
scope.InstanceSet("gorm:force_reload_after_create_attrs", hasDefaultValueColumns)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if relationship := field.Relationship; relationship != nil && relationship.Kind == "belongs_to" {
|
} else if relationship := field.Relationship; relationship != nil && relationship.Kind == "belongs_to" {
|
||||||
|
@ -98,8 +103,8 @@ func Create(scope *Scope) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ForceReloadAfterCreate(scope *Scope) {
|
func ForceReloadAfterCreate(scope *Scope) {
|
||||||
if _, ok := scope.InstanceGet("gorm:force_reload_after_create"); ok {
|
if columns, ok := scope.InstanceGet("gorm:force_reload_after_create_attrs"); ok {
|
||||||
scope.DB().New().First(scope.Value)
|
scope.DB().New().Select(columns.([]string)).First(scope.Value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -71,7 +71,9 @@ func Query(scope *Scope) {
|
||||||
if field.Field.Kind() == reflect.Ptr {
|
if field.Field.Kind() == reflect.Ptr {
|
||||||
values[index] = field.Field.Addr().Interface()
|
values[index] = field.Field.Addr().Interface()
|
||||||
} else {
|
} else {
|
||||||
values[index] = reflect.New(reflect.PtrTo(field.Field.Type())).Interface()
|
reflectValue := reflect.New(reflect.PtrTo(field.Struct.Type))
|
||||||
|
reflectValue.Elem().Set(field.Field.Addr())
|
||||||
|
values[index] = reflectValue.Interface()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
var value interface{}
|
var value interface{}
|
||||||
|
|
Loading…
Reference in New Issue