From 341703ed5d437e38e80e3ceaf7a6588766ae1fcd Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Fri, 11 Dec 2015 11:45:22 +0800 Subject: [PATCH] Scan value into ignored fields if there is no ambiguity --- field.go | 10 ++++++---- model_struct.go | 35 ++++++++++++++++++----------------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/field.go b/field.go index 9e6891c8..db1fdd8f 100644 --- a/field.go +++ b/field.go @@ -62,10 +62,12 @@ func (scope *Scope) Fields() map[string]*Field { indirectValue := scope.IndirectValue() isStruct := indirectValue.Kind() == reflect.Struct for _, structField := range modelStruct.StructFields { - if isStruct { - fields[structField.DBName] = getField(indirectValue, structField) - } else { - fields[structField.DBName] = &Field{StructField: structField, IsBlank: true} + if field, ok := fields[structField.DBName]; !ok || field.IsIgnored { + if isStruct { + fields[structField.DBName] = getField(indirectValue, structField) + } else { + fields[structField.DBName] = &Field{StructField: structField, IsBlank: true} + } } } diff --git a/model_struct.go b/model_struct.go index c4eb5827..97d145b6 100644 --- a/model_struct.go +++ b/model_struct.go @@ -149,24 +149,25 @@ func (scope *Scope) GetModelStruct() *ModelStruct { if fieldStruct.Tag.Get("sql") == "-" { field.IsIgnored = true - } else { - sqlSettings := parseTagSetting(field.Tag.Get("sql")) - gormSettings := parseTagSetting(field.Tag.Get("gorm")) - if _, ok := gormSettings["PRIMARY_KEY"]; ok { - field.IsPrimaryKey = true - modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, field) - } - - if _, ok := sqlSettings["DEFAULT"]; ok { - field.HasDefaultValue = true - } - - if value, ok := gormSettings["COLUMN"]; ok { - field.DBName = value - } else { - field.DBName = ToDBName(fieldStruct.Name) - } } + + sqlSettings := parseTagSetting(field.Tag.Get("sql")) + gormSettings := parseTagSetting(field.Tag.Get("gorm")) + if _, ok := gormSettings["PRIMARY_KEY"]; ok { + field.IsPrimaryKey = true + modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, field) + } + + if _, ok := sqlSettings["DEFAULT"]; ok { + field.HasDefaultValue = true + } + + if value, ok := gormSettings["COLUMN"]; ok { + field.DBName = value + } else { + field.DBName = ToDBName(fieldStruct.Name) + } + fields = append(fields, field) } }