Merge pull request #244 from rtfb/fix-ignored-field-bug

Fix a bug with custom columns and ignored fields
This commit is contained in:
Jinzhu 2014-10-10 08:34:32 +08:00
commit 5d0d2a8e98
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"`
}
// 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) {
col := "mapped_name"
DB.DropTable(&CustomizeColumn{})
@ -40,3 +47,8 @@ func TestCustomizeColumn(t *testing.T) {
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 {
hasPrimaryKey = true
}
if field.IsIgnored {
continue
}
if _, ok := fields[field.DBName]; ok {
panic(fmt.Sprintf("Duplicated column name for %v (%v)\n", scope.typeName(), fileWithLineNum()))
} else {