Support inline condition when Delete

This commit is contained in:
Jinzhu 2014-07-24 18:30:12 +08:00
parent ae12a1cca5
commit 11dc47587c
2 changed files with 25 additions and 2 deletions

View File

@ -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 {

View File

@ -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})