Support Unscoped when delete with selected associations, close #4062

This commit is contained in:
Jinzhu 2021-02-07 14:36:27 +08:00
parent deff0594ee
commit 883c32e59a
2 changed files with 27 additions and 0 deletions

View File

@ -36,6 +36,9 @@ func DeleteBeforeAssociations(db *gorm.DB) {
modelValue := reflect.New(rel.FieldSchema.ModelType).Interface()
tx := db.Session(&gorm.Session{NewDB: true}).Model(modelValue)
withoutConditions := false
if db.Statement.Unscoped {
tx = tx.Unscoped()
}
if len(db.Statement.Selects) > 0 {
var selects []string

View File

@ -153,6 +153,30 @@ func TestDeleteWithAssociations(t *testing.T) {
}
}
func TestDeleteAssociationsWithUnscoped(t *testing.T) {
user := GetUser("unscoped_delete_with_associations", Config{Account: true, Pets: 2, Toys: 4, Company: true, Manager: true, Team: 1, Languages: 1, Friends: 1})
if err := DB.Create(user).Error; err != nil {
t.Fatalf("failed to create user, got error %v", err)
}
if err := DB.Unscoped().Select(clause.Associations, "Pets.Toy").Delete(&user).Error; err != nil {
t.Fatalf("failed to delete user, got error %v", err)
}
for key, value := range map[string]int64{"Account": 0, "Pets": 0, "Toys": 0, "Company": 1, "Manager": 1, "Team": 0, "Languages": 0, "Friends": 0} {
if count := DB.Unscoped().Model(&user).Association(key).Count(); count != value {
t.Errorf("user's %v expects: %v, got %v", key, value, count)
}
}
for key, value := range map[string]int64{"Account": 0, "Pets": 0, "Toys": 0, "Company": 1, "Manager": 1, "Team": 0, "Languages": 0, "Friends": 0} {
if count := DB.Model(&user).Association(key).Count(); count != value {
t.Errorf("user's %v expects: %v, got %v", key, value, count)
}
}
}
func TestDeleteSliceWithAssociations(t *testing.T) {
users := []User{
*GetUser("delete_slice_with_associations1", Config{Account: true, Pets: 4, Toys: 1, Company: true, Manager: true, Team: 1, Languages: 1, Friends: 4}),