diff --git a/callbacks/delete.go b/callbacks/delete.go index ff0f601a..51a33bf0 100644 --- a/callbacks/delete.go +++ b/callbacks/delete.go @@ -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}}}) diff --git a/soft_delete.go b/soft_delete.go index e3e6e960..6b88b1a5 100644 --- a/soft_delete.go +++ b/soft_delete.go @@ -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}}}) diff --git a/tests/delete_test.go b/tests/delete_test.go index b853a9d3..3d461f65 100644 --- a/tests/delete_test.go +++ b/tests/delete_test.go @@ -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{})