Fix a bug with custom columns and ignored fields

An ignored field should also be ignored when column names are being
determined.
This commit is contained in:
Vytautas Šaltenis 2014-10-10 00:05:15 +03:00
parent 0aaefebf4f
commit 67f71b2f86
2 changed files with 15 additions and 0 deletions

View File

@ -11,6 +11,13 @@ type CustomizeColumn struct {
Date time.Time `gorm:"column:mapped_time"` Date time.Time `gorm:"column:mapped_time"`
} }
// Make sure an ignored field does not interfere with another field's custom
// column name that matches the ignored field.
type CustomColumnAndIgnoredFieldClash struct {
Body string `sql:"-"`
RawBody string `gorm:"column:body"`
}
func TestCustomizeColumn(t *testing.T) { func TestCustomizeColumn(t *testing.T) {
col := "mapped_name" col := "mapped_name"
DB.DropTable(&CustomizeColumn{}) DB.DropTable(&CustomizeColumn{})
@ -40,3 +47,8 @@ func TestCustomizeColumn(t *testing.T) {
t.Errorf("Failed to query CustomizeColumn") t.Errorf("Failed to query CustomizeColumn")
} }
} }
func TestCustomColumnAndIgnoredFieldClash(t *testing.T) {
DB.DropTable(&CustomColumnAndIgnoredFieldClash{})
DB.AutoMigrate(&CustomColumnAndIgnoredFieldClash{})
}

View File

@ -414,6 +414,9 @@ func (scope *Scope) Fields(noRelations ...bool) map[string]*Field {
if field.IsPrimaryKey { if field.IsPrimaryKey {
hasPrimaryKey = true hasPrimaryKey = true
} }
if field.IsIgnored {
continue
}
if _, ok := fields[field.DBName]; ok { if _, ok := fields[field.DBName]; ok {
panic(fmt.Sprintf("Duplicated column name for %v (%v)\n", scope.typeName(), fileWithLineNum())) panic(fmt.Sprintf("Duplicated column name for %v (%v)\n", scope.typeName(), fileWithLineNum()))
} else { } else {