mirror of https://github.com/go-gorm/gorm.git
Fix can't update record with customized primary key
This commit is contained in:
parent
a29ac54e48
commit
010e7a90b3
|
@ -6,7 +6,7 @@ import (
|
|||
)
|
||||
|
||||
type CustomizeColumn struct {
|
||||
Id int64 `gorm:"column:mapped_id; primary_key:yes"`
|
||||
ID int64 `gorm:"column:mapped_id; primary_key:yes"`
|
||||
Name string `gorm:"column:mapped_name"`
|
||||
Date time.Time `gorm:"column:mapped_time"`
|
||||
}
|
||||
|
@ -34,16 +34,25 @@ func TestCustomizeColumn(t *testing.T) {
|
|||
}
|
||||
|
||||
expected := "foo"
|
||||
cc := CustomizeColumn{Id: 666, Name: expected, Date: time.Now()}
|
||||
cc := CustomizeColumn{ID: 666, Name: expected, Date: time.Now()}
|
||||
|
||||
if count := DB.Save(&cc).RowsAffected; count != 1 {
|
||||
if count := DB.Create(&cc).RowsAffected; count != 1 {
|
||||
t.Error("There should be one record be affected when create record")
|
||||
}
|
||||
|
||||
var ccs []CustomizeColumn
|
||||
DB.Find(&ccs)
|
||||
var cc1 CustomizeColumn
|
||||
DB.First(&cc1, 666)
|
||||
|
||||
if len(ccs) > 0 && ccs[0].Name != expected && ccs[0].Id != 666 {
|
||||
if cc1.Name != expected {
|
||||
t.Errorf("Failed to query CustomizeColumn")
|
||||
}
|
||||
|
||||
cc.Name = "bar"
|
||||
DB.Save(&cc)
|
||||
|
||||
var cc2 CustomizeColumn
|
||||
DB.First(&cc2, 666)
|
||||
if cc2.Name != "bar" {
|
||||
t.Errorf("Failed to query CustomizeColumn")
|
||||
}
|
||||
}
|
||||
|
|
33
scope.go
33
scope.go
|
@ -19,7 +19,7 @@ type Scope struct {
|
|||
SqlVars []interface{}
|
||||
db *DB
|
||||
skipLeft bool
|
||||
primaryKey string
|
||||
primaryKeyField *Field
|
||||
instanceId string
|
||||
fields map[string]*Field
|
||||
}
|
||||
|
@ -90,12 +90,8 @@ func (scope *Scope) HasError() bool {
|
|||
return scope.db.Error != nil
|
||||
}
|
||||
|
||||
// PrimaryKey get the primary key's column name
|
||||
func (scope *Scope) PrimaryKey() string {
|
||||
if scope.primaryKey != "" {
|
||||
return scope.primaryKey
|
||||
}
|
||||
|
||||
func (scope *Scope) PrimaryKeyField() *Field {
|
||||
if scope.primaryKeyField == nil {
|
||||
var indirectValue = scope.IndirectValue()
|
||||
|
||||
clone := scope
|
||||
|
@ -105,12 +101,22 @@ func (scope *Scope) PrimaryKey() string {
|
|||
|
||||
for _, field := range clone.Fields() {
|
||||
if field.IsPrimaryKey {
|
||||
scope.primaryKey = field.DBName
|
||||
scope.primaryKeyField = field
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return scope.primaryKey
|
||||
return scope.primaryKeyField
|
||||
}
|
||||
|
||||
// PrimaryKey get the primary key's column name
|
||||
func (scope *Scope) PrimaryKey() string {
|
||||
if field := scope.PrimaryKeyField(); field != nil {
|
||||
return field.DBName
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
// PrimaryKeyZero check the primary key is blank or not
|
||||
|
@ -120,13 +126,12 @@ func (scope *Scope) PrimaryKeyZero() bool {
|
|||
|
||||
// PrimaryKeyValue get the primary key's value
|
||||
func (scope *Scope) PrimaryKeyValue() interface{} {
|
||||
if scope.IndirectValue().Kind() == reflect.Struct {
|
||||
if field := scope.IndirectValue().FieldByName(SnakeToUpperCamel(scope.PrimaryKey())); field.IsValid() {
|
||||
return field.Interface()
|
||||
}
|
||||
}
|
||||
if field := scope.PrimaryKeyField(); field != nil {
|
||||
return field.Field.Interface()
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// HasColumn to check if has column
|
||||
func (scope *Scope) HasColumn(column string) (hasColumn bool) {
|
||||
|
|
Loading…
Reference in New Issue