mirror of https://github.com/go-gorm/gorm.git
Fix customize table with Delete, close #3129
This commit is contained in:
parent
c0319f6eed
commit
33c48611b6
|
@ -35,7 +35,7 @@ func Delete(db *gorm.DB) {
|
||||||
|
|
||||||
if db.Statement.Schema != nil {
|
if db.Statement.Schema != nil {
|
||||||
_, queryValues := schema.GetIdentityFieldValuesMap(db.Statement.ReflectValue, db.Statement.Schema.PrimaryFields)
|
_, 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 {
|
if len(values) > 0 {
|
||||||
db.Statement.AddClause(clause.Where{Exprs: []clause.Expression{clause.IN{Column: column, Values: values}}})
|
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 {
|
if db.Statement.Dest != db.Statement.Model && db.Statement.Model != nil {
|
||||||
_, queryValues = schema.GetIdentityFieldValuesMap(reflect.ValueOf(db.Statement.Model), db.Statement.Schema.PrimaryFields)
|
_, 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 {
|
if len(values) > 0 {
|
||||||
db.Statement.AddClause(clause.Where{Exprs: []clause.Expression{clause.IN{Column: column, Values: values}}})
|
db.Statement.AddClause(clause.Where{Exprs: []clause.Expression{clause.IN{Column: column, Values: values}}})
|
||||||
|
|
|
@ -58,7 +58,7 @@ func (SoftDeleteClause) ModifyStatement(stmt *Statement) {
|
||||||
|
|
||||||
if stmt.Schema != nil {
|
if stmt.Schema != nil {
|
||||||
_, queryValues := schema.GetIdentityFieldValuesMap(stmt.ReflectValue, stmt.Schema.PrimaryFields)
|
_, 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 {
|
if len(values) > 0 {
|
||||||
stmt.AddClause(clause.Where{Exprs: []clause.Expression{clause.IN{Column: column, Values: values}}})
|
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 {
|
if stmt.Dest != stmt.Model && stmt.Model != nil {
|
||||||
_, queryValues = schema.GetIdentityFieldValuesMap(reflect.ValueOf(stmt.Model), stmt.Schema.PrimaryFields)
|
_, 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 {
|
if len(values) > 0 {
|
||||||
stmt.AddClause(clause.Where{Exprs: []clause.Expression{clause.IN{Column: column, Values: values}}})
|
stmt.AddClause(clause.Where{Exprs: []clause.Expression{clause.IN{Column: column, Values: values}}})
|
||||||
|
|
|
@ -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) {
|
func TestInlineCondDelete(t *testing.T) {
|
||||||
user1 := *GetUser("inline_delete_1", Config{})
|
user1 := *GetUser("inline_delete_1", Config{})
|
||||||
user2 := *GetUser("inline_delete_2", Config{})
|
user2 := *GetUser("inline_delete_2", Config{})
|
||||||
|
|
Loading…
Reference in New Issue