mirror of https://github.com/go-gorm/gorm.git
Merge pull request #244 from rtfb/fix-ignored-field-bug
Fix a bug with custom columns and ignored fields
This commit is contained in:
commit
5d0d2a8e98
|
@ -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{})
|
||||
}
|
||||
|
|
3
scope.go
3
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 {
|
||||
|
|
Loading…
Reference in New Issue