From b33f30714baeee37211f857f7135c78a394ed8d0 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 5 Feb 2015 17:42:24 +0800 Subject: [PATCH] Fix Error when using custom columns with ignored fields --- customize_column_test.go | 4 +++- scope_private.go | 14 +++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/customize_column_test.go b/customize_column_test.go index aec3f39e..f5a31a5b 100644 --- a/customize_column_test.go +++ b/customize_column_test.go @@ -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) + } } diff --git a/scope_private.go b/scope_private.go index 4c457355..79e00ee2 100644 --- a/scope_private.go +++ b/scope_private.go @@ -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()