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) {
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
}
for _, field := range scope.fieldFromStruct(scopeType.Field(i), false) {
field = fields[field.DBName]
if field.IsNormal {
sqlTag := scope.sqlTagForField(field)
sqls = append(sqls, scope.Quote(field.DBName)+" "+sqlTag)
name := field.Name
for _, field := range fields {
if field.Name == name {
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()