Fix customize table with Delete, close #3129

This commit is contained in:
Jinzhu 2020-07-10 13:08:15 +08:00
parent c0319f6eed
commit 33c48611b6
3 changed files with 47 additions and 4 deletions

View File

@ -35,7 +35,7 @@ func Delete(db *gorm.DB) {
if db.Statement.Schema != nil {
_, queryValues := schema.GetIdentityFieldValuesMap(db.Statement.ReflectValue, db.Statement.Schema.PrimaryFields)
column, values := schema.ToQueryValues(db.Statement.Schema.Table, db.Statement.Schema.PrimaryFieldDBNames, queryValues)
column, values := schema.ToQueryValues(db.Statement.Table, db.Statement.Schema.PrimaryFieldDBNames, queryValues)
if len(values) > 0 {
db.Statement.AddClause(clause.Where{Exprs: []clause.Expression{clause.IN{Column: column, Values: values}}})
@ -43,7 +43,7 @@ func Delete(db *gorm.DB) {
if db.Statement.Dest != db.Statement.Model && db.Statement.Model != nil {
_, queryValues = schema.GetIdentityFieldValuesMap(reflect.ValueOf(db.Statement.Model), db.Statement.Schema.PrimaryFields)
column, values = schema.ToQueryValues(db.Statement.Schema.Table, db.Statement.Schema.PrimaryFieldDBNames, queryValues)
column, values = schema.ToQueryValues(db.Statement.Table, db.Statement.Schema.PrimaryFieldDBNames, queryValues)
if len(values) > 0 {
db.Statement.AddClause(clause.Where{Exprs: []clause.Expression{clause.IN{Column: column, Values: values}}})

View File

@ -58,7 +58,7 @@ func (SoftDeleteClause) ModifyStatement(stmt *Statement) {
if stmt.Schema != nil {
_, queryValues := schema.GetIdentityFieldValuesMap(stmt.ReflectValue, stmt.Schema.PrimaryFields)
column, values := schema.ToQueryValues(stmt.Schema.Table, stmt.Schema.PrimaryFieldDBNames, queryValues)
column, values := schema.ToQueryValues(stmt.Table, stmt.Schema.PrimaryFieldDBNames, queryValues)
if len(values) > 0 {
stmt.AddClause(clause.Where{Exprs: []clause.Expression{clause.IN{Column: column, Values: values}}})
@ -66,7 +66,7 @@ func (SoftDeleteClause) ModifyStatement(stmt *Statement) {
if stmt.Dest != stmt.Model && stmt.Model != nil {
_, queryValues = schema.GetIdentityFieldValuesMap(reflect.ValueOf(stmt.Model), stmt.Schema.PrimaryFields)
column, values = schema.ToQueryValues(stmt.Schema.Table, stmt.Schema.PrimaryFieldDBNames, queryValues)
column, values = schema.ToQueryValues(stmt.Table, stmt.Schema.PrimaryFieldDBNames, queryValues)
if len(values) > 0 {
stmt.AddClause(clause.Where{Exprs: []clause.Expression{clause.IN{Column: column, Values: values}}})

View File

@ -45,6 +45,49 @@ func TestDelete(t *testing.T) {
}
}
func TestDeleteWithTable(t *testing.T) {
type UserWithDelete struct {
gorm.Model
Name string
}
DB.Table("deleted_users").Migrator().DropTable(UserWithDelete{})
DB.Table("deleted_users").AutoMigrate(UserWithDelete{})
user := UserWithDelete{Name: "delete1"}
DB.Table("deleted_users").Create(&user)
var result UserWithDelete
if err := DB.Table("deleted_users").First(&result).Error; err != nil {
t.Errorf("failed to find deleted user, got error %v", err)
}
AssertEqual(t, result, user)
if err := DB.Table("deleted_users").Delete(&result).Error; err != nil {
t.Errorf("failed to delete user, got error %v", err)
}
var result2 UserWithDelete
if err := DB.Table("deleted_users").First(&result2, user.ID).Error; !errors.Is(err, gorm.ErrRecordNotFound) {
t.Errorf("should raise record not found error, but got error %v", err)
}
var result3 UserWithDelete
if err := DB.Table("deleted_users").Unscoped().First(&result3, user.ID).Error; err != nil {
t.Fatalf("failed to find record, got error %v", err)
}
if err := DB.Table("deleted_users").Unscoped().Delete(&result).Error; err != nil {
t.Errorf("failed to delete user with unscoped, got error %v", err)
}
var result4 UserWithDelete
if err := DB.Table("deleted_users").Unscoped().First(&result4, user.ID).Error; !errors.Is(err, gorm.ErrRecordNotFound) {
t.Errorf("should raise record not found error, but got error %v", err)
}
}
func TestInlineCondDelete(t *testing.T) {
user1 := *GetUser("inline_delete_1", Config{})
user2 := *GetUser("inline_delete_2", Config{})