mirror of https://github.com/go-gorm/gorm.git
Fix can't load preload fields correctly
This commit is contained in:
parent
198fc47051
commit
37bf87aa29
9
field.go
9
field.go
|
@ -56,11 +56,11 @@ func (field *Field) Set(value interface{}) error {
|
|||
func (scope *Scope) Fields() map[string]*Field {
|
||||
if scope.fields == nil {
|
||||
fields := map[string]*Field{}
|
||||
structFields := scope.GetStructFields()
|
||||
modelStruct := scope.GetModelStruct()
|
||||
|
||||
indirectValue := scope.IndirectValue()
|
||||
isStruct := indirectValue.Kind() == reflect.Struct
|
||||
for _, structField := range structFields {
|
||||
for _, structField := range modelStruct.StructFields {
|
||||
if isStruct {
|
||||
fields[structField.DBName] = getField(indirectValue, structField)
|
||||
} else {
|
||||
|
@ -68,7 +68,10 @@ func (scope *Scope) Fields() map[string]*Field {
|
|||
}
|
||||
}
|
||||
|
||||
scope.fields = fields
|
||||
if modelStruct.cached {
|
||||
scope.fields = fields
|
||||
}
|
||||
return fields
|
||||
}
|
||||
return scope.fields
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ type ModelStruct struct {
|
|||
StructFields []*StructField
|
||||
ModelType reflect.Type
|
||||
defaultTableName string
|
||||
cached bool
|
||||
}
|
||||
|
||||
func (s ModelStruct) TableName(db *DB) string {
|
||||
|
@ -372,6 +373,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||
modelStructs[scopeType] = &modelStruct
|
||||
|
||||
<-finished
|
||||
modelStruct.cached = true
|
||||
|
||||
return &modelStruct
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue