forked from mirror/gorm
Cascade delete associations, close #3473
This commit is contained in:
parent
1d5f910b6e
commit
06d534d6ea
|
@ -34,9 +34,24 @@ func DeleteBeforeAssociations(db *gorm.DB) {
|
||||||
queryConds := rel.ToQueryConditions(db.Statement.ReflectValue)
|
queryConds := rel.ToQueryConditions(db.Statement.ReflectValue)
|
||||||
modelValue := reflect.New(rel.FieldSchema.ModelType).Interface()
|
modelValue := reflect.New(rel.FieldSchema.ModelType).Interface()
|
||||||
tx := db.Session(&gorm.Session{}).Model(modelValue)
|
tx := db.Session(&gorm.Session{}).Model(modelValue)
|
||||||
|
withoutConditions := false
|
||||||
|
|
||||||
|
if len(db.Statement.Selects) > 0 {
|
||||||
|
tx = tx.Select(db.Statement.Selects)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, cond := range queryConds {
|
||||||
|
if c, ok := cond.(clause.IN); ok && len(c.Values) == 0 {
|
||||||
|
withoutConditions = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !withoutConditions {
|
||||||
if db.AddError(tx.Clauses(clause.Where{Exprs: queryConds}).Delete(modelValue).Error) != nil {
|
if db.AddError(tx.Clauses(clause.Where{Exprs: queryConds}).Delete(modelValue).Error) != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
case schema.Many2Many:
|
case schema.Many2Many:
|
||||||
var (
|
var (
|
||||||
queryConds []clause.Expression
|
queryConds []clause.Expression
|
||||||
|
|
Loading…
Reference in New Issue