1. fix updates with primary key bug
This commit is contained in:
parent
fb48d751af
commit
0a21873c6e
@ -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
|
||||||
|
9
main.go
9
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 {
|
||||||
|
scope := s.clone().NewScope(values)
|
||||||
|
// if not primary key ,use old updates rule
|
||||||
|
if scope.PrimaryKeyZero() {
|
||||||
return s.clone().NewScope(s.Value).
|
return s.clone().NewScope(s.Value).
|
||||||
Set("gorm:ignore_protected_attrs", len(ignoreProtectedAttrs) > 0).
|
Set("gorm:ignore_protected_attrs", len(ignoreProtectedAttrs) > 0).
|
||||||
InstanceSet("gorm:update_interface", values).
|
InstanceSet("gorm:update_interface", values).
|
||||||
callCallbacks(s.parent.callbacks.updates).db
|
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user