Fix Error when using custom columns with ignored fields

This commit is contained in:
Jinzhu 2015-02-05 17:42:24 +08:00
parent 1888335b6e
commit b33f30714b
2 changed files with 12 additions and 6 deletions

View File

@ -59,5 +59,7 @@ func TestCustomizeColumn(t *testing.T) {
func TestCustomColumnAndIgnoredFieldClash(t *testing.T) { func TestCustomColumnAndIgnoredFieldClash(t *testing.T) {
DB.DropTable(&CustomColumnAndIgnoredFieldClash{}) DB.DropTable(&CustomColumnAndIgnoredFieldClash{})
DB.AutoMigrate(&CustomColumnAndIgnoredFieldClash{}) if err := DB.AutoMigrate(&CustomColumnAndIgnoredFieldClash{}).Error; err != nil {
t.Errorf("Should not raise error: %s", err)
}
} }

View File

@ -582,7 +582,9 @@ func (scope *Scope) createTable() *Scope {
continue continue
} }
for _, field := range scope.fieldFromStruct(scopeType.Field(i), false) { for _, field := range scope.fieldFromStruct(scopeType.Field(i), false) {
field = fields[field.DBName] name := field.Name
for _, field := range fields {
if field.Name == name {
if field.IsNormal { if field.IsNormal {
sqlTag := scope.sqlTagForField(field) sqlTag := scope.sqlTagForField(field)
sqls = append(sqls, scope.Quote(field.DBName)+" "+sqlTag) sqls = append(sqls, scope.Quote(field.DBName)+" "+sqlTag)
@ -590,6 +592,8 @@ func (scope *Scope) createTable() *Scope {
scope.createJoinTable(field) scope.createJoinTable(field)
} }
} }
}
}
scope.Raw(fmt.Sprintf("CREATE TABLE %v (%v)", scope.QuotedTableName(), strings.Join(sqls, ","))).Exec() scope.Raw(fmt.Sprintf("CREATE TABLE %v (%v)", scope.QuotedTableName(), strings.Join(sqls, ","))).Exec()
return scope return scope
} }