From 67f71b2f86ee23c5e503bdf71da4bf51a07ce33b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytautas=20=C5=A0altenis?= Date: Fri, 10 Oct 2014 00:05:15 +0300 Subject: [PATCH] Fix a bug with custom columns and ignored fields An ignored field should also be ignored when column names are being determined. --- customize_column_test.go | 12 ++++++++++++ scope.go | 3 +++ 2 files changed, 15 insertions(+) diff --git a/customize_column_test.go b/customize_column_test.go index 5c63e9d6..c7f72f97 100644 --- a/customize_column_test.go +++ b/customize_column_test.go @@ -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{}) +} diff --git a/scope.go b/scope.go index 006259a8..242f473a 100644 --- a/scope.go +++ b/scope.go @@ -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 {