diff --git a/model_struct.go b/model_struct.go index d9e2e90f..19cfe7d0 100644 --- a/model_struct.go +++ b/model_struct.go @@ -633,6 +633,28 @@ func (scope *Scope) GetModelStruct() *ModelStruct { } } + // look for field which has tag COLUMN assigned, if found any, set other fields which has the same DBName, while have no COLUMN tag assigned, to IsIgnored = true + for _, v := range modelStruct.StructFields { + if column, ok := v.TagSettingsGet("COLUMN"); ok { + for k, field := range modelStruct.StructFields { + if _, ok := field.TagSettingsGet("COLUMN"); ok { + continue + } + if field.DBName == column { + ignoredField := &StructField{ + Struct: field.Struct, + Name: field.Name, + Names: field.Names, + Tag: field.Tag, + TagSettings: field.TagSettings, + IsIgnored: true, + } + modelStruct.StructFields[k] = ignoredField + } + } + } + } + if len(modelStruct.PrimaryFields) == 0 { if field := getForeignField("id", modelStruct.StructFields); field != nil { field.IsPrimaryKey = true diff --git a/model_struct_test.go b/model_struct_test.go index 2ae419a0..79d31352 100644 --- a/model_struct_test.go +++ b/model_struct_test.go @@ -91,3 +91,23 @@ func TestModelStructRaceDifferentModel(t *testing.T) { done.Wait() } + +type ModelSameColumnName struct { + gorm.Model + ID string `gorm:"column:id"` +} + +func TestModelStructIgnoreNoTagSameDBNameColumn(t *testing.T) { + sf := DB.NewScope(&ModelSameColumnName{}).GetStructFields() + + columnIDcount := 0 + for _, v := range sf { + if v.DBName == "id" { + columnIDcount++ + } + } + + if columnIDcount > 1 { + t.Fatal("fields have same name while contains no column tag not being ignored") + } +}