Merge 0a21873c6ea2f4b262b5babe27f1994be63fa8bb into 77eb925ea09471b7082d9d5749b2c96be726eac2
This commit is contained in:
commit
7b62ad8873
@ -16,6 +16,8 @@ var (
|
|||||||
ErrCantStartTransaction = errors.New("can't start transaction")
|
ErrCantStartTransaction = errors.New("can't start transaction")
|
||||||
// ErrUnaddressable unaddressable value
|
// ErrUnaddressable unaddressable value
|
||||||
ErrUnaddressable = errors.New("using unaddressable value")
|
ErrUnaddressable = errors.New("using unaddressable value")
|
||||||
|
// ErrUpdateWithInvalidPrimaryKey ,when update with a invalid primary key
|
||||||
|
ErrUpdateWithInvalidPrimaryKey = errors.New("invalid primary key")
|
||||||
)
|
)
|
||||||
|
|
||||||
// Errors contains all happened errors
|
// Errors contains all happened errors
|
||||||
|
17
main.go
17
main.go
@ -377,10 +377,19 @@ func (s *DB) Update(attrs ...interface{}) *DB {
|
|||||||
|
|
||||||
// Updates update attributes with callbacks, refer: https://jinzhu.github.io/gorm/crud.html#update
|
// Updates update attributes with callbacks, refer: https://jinzhu.github.io/gorm/crud.html#update
|
||||||
func (s *DB) Updates(values interface{}, ignoreProtectedAttrs ...bool) *DB {
|
func (s *DB) Updates(values interface{}, ignoreProtectedAttrs ...bool) *DB {
|
||||||
return s.clone().NewScope(s.Value).
|
scope := s.clone().NewScope(values)
|
||||||
Set("gorm:ignore_protected_attrs", len(ignoreProtectedAttrs) > 0).
|
// if not primary key ,use old updates rule
|
||||||
InstanceSet("gorm:update_interface", values).
|
if scope.PrimaryKeyZero() {
|
||||||
callCallbacks(s.parent.callbacks.updates).db
|
return s.clone().NewScope(s.Value).
|
||||||
|
Set("gorm:ignore_protected_attrs", len(ignoreProtectedAttrs) > 0).
|
||||||
|
InstanceSet("gorm:update_interface", values).
|
||||||
|
callCallbacks(s.parent.callbacks.updates).db
|
||||||
|
}
|
||||||
|
newDB := scope.callCallbacks(s.parent.callbacks.updates).db
|
||||||
|
if newDB.Error == nil && newDB.RowsAffected == 0 {
|
||||||
|
newDB.AddError(ErrUpdateWithInvalidPrimaryKey)
|
||||||
|
}
|
||||||
|
return newDB
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateColumn update attributes without callbacks, refer: https://jinzhu.github.io/gorm/crud.html#update
|
// UpdateColumn update attributes without callbacks, refer: https://jinzhu.github.io/gorm/crud.html#update
|
||||||
|
30
main_test.go
30
main_test.go
@ -861,6 +861,36 @@ func TestBlockGlobalUpdate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUpdatePrimaryKey(t *testing.T) {
|
||||||
|
type Product struct {
|
||||||
|
gorm.Model
|
||||||
|
Name string
|
||||||
|
Price int
|
||||||
|
}
|
||||||
|
type LightItem struct {
|
||||||
|
gorm.Model
|
||||||
|
Name string
|
||||||
|
MyProductID uint `gorm:"index"`
|
||||||
|
MyProduct Product
|
||||||
|
}
|
||||||
|
tables := []interface{}{&LightItem{}, &Product{}}
|
||||||
|
for _, t := range tables {
|
||||||
|
DB.DropTableIfExists(t)
|
||||||
|
DB.CreateTable(t)
|
||||||
|
}
|
||||||
|
p := Product{Name: "mi6", Price: 2499}
|
||||||
|
if err := DB.Save(&p).Error; err != nil {
|
||||||
|
t.Errorf("Failure to Save with product(%v)", err)
|
||||||
|
}
|
||||||
|
l := LightItem{Name: "line", MyProductID: p.ID}
|
||||||
|
if err := DB.Save(&l).Error; err != nil {
|
||||||
|
t.Errorf("Failure to Save with light item(%v)", err)
|
||||||
|
}
|
||||||
|
if err := DB.Updates(&l).Error; err != nil {
|
||||||
|
t.Errorf("Failure to update that have primary key object:reason(%v)", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkGorm(b *testing.B) {
|
func BenchmarkGorm(b *testing.B) {
|
||||||
b.N = 2000
|
b.N = 2000
|
||||||
for x := 0; x < b.N; x++ {
|
for x := 0; x < b.N; x++ {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user