mirror of https://github.com/go-gorm/gorm.git
Support inline condition when Delete
This commit is contained in:
parent
ae12a1cca5
commit
11dc47587c
4
main.go
4
main.go
|
@ -227,8 +227,8 @@ func (s *DB) Create(value interface{}) *DB {
|
|||
return scope.callCallbacks(s.parent.callback.creates).db
|
||||
}
|
||||
|
||||
func (s *DB) Delete(value interface{}) *DB {
|
||||
return s.clone().NewScope(value).callCallbacks(s.parent.callback.deletes).db
|
||||
func (s *DB) Delete(value interface{}, where ...interface{}) *DB {
|
||||
return s.clone().NewScope(value).inlineCondition(where...).callCallbacks(s.parent.callback.deletes).db
|
||||
}
|
||||
|
||||
func (s *DB) Raw(sql string, values ...interface{}) *DB {
|
||||
|
|
23
main_test.go
23
main_test.go
|
@ -407,6 +407,29 @@ func TestDelete(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestInlineDelete(t *testing.T) {
|
||||
name, name2 := "inline_delete", "inline_delete2"
|
||||
user := User{Name: name, Age: 1}
|
||||
db.Save(&user)
|
||||
db.Save(&User{Name: name2, Age: 1})
|
||||
|
||||
if db.Delete(&User{}, user.Id).Error != nil {
|
||||
t.Errorf("Shouldn't raise any error when delete a user")
|
||||
}
|
||||
|
||||
if db.Where("name = ?", name).First(&User{}).Error == nil {
|
||||
t.Errorf("User can't be found after delete")
|
||||
}
|
||||
|
||||
if db.Delete(&User{}, "name = ?", name2).Error != nil {
|
||||
t.Errorf("Shouldn't raise any error when delete a user")
|
||||
}
|
||||
|
||||
if db.Where("name = ?", name2).First(&User{}).Error == nil {
|
||||
t.Errorf("User can't be found after delete")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWhere(t *testing.T) {
|
||||
name := "where"
|
||||
db.Save(&User{Name: name, Age: 1})
|
||||
|
|
Loading…
Reference in New Issue