diff --git a/field.go b/field.go index 8f5efa6d..a56dbe51 100644 --- a/field.go +++ b/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 } diff --git a/model_struct.go b/model_struct.go index 608c70d4..f5fc5797 100644 --- a/model_struct.go +++ b/model_struct.go @@ -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 }