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,12 +582,16 @@ 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
if field.IsNormal { for _, field := range fields {
sqlTag := scope.sqlTagForField(field) if field.Name == name {
sqls = append(sqls, scope.Quote(field.DBName)+" "+sqlTag) if field.IsNormal {
sqlTag := scope.sqlTagForField(field)
sqls = append(sqls, scope.Quote(field.DBName)+" "+sqlTag)
}
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()