Add support for custom column names

This commit is contained in:
Jinzhu 2014-10-07 22:33:57 +08:00
parent 5b5ee62d06
commit 0aaefebf4f
3 changed files with 72 additions and 8 deletions

View File

@ -979,6 +979,16 @@ type Animal struct {
}
```
If your column names differ from the struct fields, you can specify them like this:
```go
type Animal struct {
AnimalId int64 `gorm:"column:beast_id; primary_key:yes"`
Birthday time.Time `gorm:"column:day_of_the_beast"`
Age int64 `gorm:"column:age_of_the_beast"`
}
```
## More examples with query chain
```go

42
customize_column_test.go Normal file
View File

@ -0,0 +1,42 @@
package gorm_test
import (
"testing"
"time"
)
type CustomizeColumn struct {
Id int64 `gorm:"column:mapped_id; primary_key:yes"`
Name string `gorm:"column:mapped_name"`
Date time.Time `gorm:"column:mapped_time"`
}
func TestCustomizeColumn(t *testing.T) {
col := "mapped_name"
DB.DropTable(&CustomizeColumn{})
DB.AutoMigrate(&CustomizeColumn{})
scope := DB.Model("").NewScope(&CustomizeColumn{})
if !scope.Dialect().HasColumn(scope, scope.TableName(), col) {
t.Errorf("CustomizeColumn should have column %s", col)
}
col = "mapped_id"
if scope.PrimaryKey() != col {
t.Errorf("CustomizeColumn should have primary key %s, but got %q", col, scope.PrimaryKey())
}
expected := "foo"
cc := CustomizeColumn{Id: 666, Name: expected, Date: time.Now()}
if count := DB.Save(&cc).RowsAffected; count != 1 {
t.Error("There should be one record be affected when create record")
}
var ccs []CustomizeColumn
DB.Find(&ccs)
if len(ccs) > 0 && ccs[0].Name != expected && ccs[0].Id != 666 {
t.Errorf("Failed to query CustomizeColumn")
}
}

View File

@ -175,13 +175,20 @@ func (scope *Scope) CallMethod(name string) {
call := func(value interface{}) {
if fm := reflect.ValueOf(value).MethodByName(name); fm.IsValid() {
switch f := fm.Interface().(type) {
case func(): f()
case func(s *Scope): f(scope)
case func(s *DB): f(scope.db.new())
case func() error: scope.Err(f())
case func(s *Scope) error: scope.Err(f(scope))
case func(s *DB) error: scope.Err(f(scope.db.new()))
default: scope.Err(errors.New(fmt.Sprintf("unsupported function %v", name)))
case func():
f()
case func(s *Scope):
f(scope)
case func(s *DB):
f(scope.db.new())
case func() error:
scope.Err(f())
case func(s *Scope) error:
scope.Err(f(scope))
case func(s *DB) error:
scope.Err(f(scope.db.new()))
default:
scope.Err(errors.New(fmt.Sprintf("unsupported function %v", name)))
}
}
}
@ -275,7 +282,6 @@ func (scope *Scope) FieldByName(name string) (field *Field, ok bool) {
func (scope *Scope) fieldFromStruct(fieldStruct reflect.StructField, withRelation bool) []*Field {
var field Field
field.Name = fieldStruct.Name
field.DBName = ToSnake(fieldStruct.Name)
value := scope.IndirectValue().FieldByName(fieldStruct.Name)
indirectValue := reflect.Indirect(value)
@ -290,6 +296,12 @@ func (scope *Scope) fieldFromStruct(fieldStruct reflect.StructField, withRelatio
field.Tag = fieldStruct.Tag
if value, ok := settings["COLUMN"]; ok {
field.DBName = value
} else {
field.DBName = ToSnake(fieldStruct.Name)
}
tagIdentifier := "sql"
if scope.db != nil {
tagIdentifier = scope.db.parent.tagIdentifier