mirror of https://github.com/go-gorm/gorm.git
Fix association Replace delete wrong records
This commit is contained in:
parent
2c76c4983d
commit
8aef600540
|
@ -137,8 +137,11 @@ func (association *Association) Replace(values ...interface{}) *Association {
|
||||||
addedPrimaryKeys = append(addedPrimaryKeys, primaryKey)
|
addedPrimaryKeys = append(addedPrimaryKeys, primaryKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
whereSql := fmt.Sprintf("%v.%v NOT IN (?)", relationship.JoinTable, scope.Quote(ToSnake(relationship.AssociationForeignKey)))
|
whereSql := fmt.Sprintf("%v.%v = ? AND %v.%v NOT IN (?)",
|
||||||
scope.db.Model("").Table(relationship.JoinTable).Where(whereSql, addedPrimaryKeys).Delete("")
|
relationship.JoinTable, association.Scope.Quote(ToSnake(relationship.ForeignKey)),
|
||||||
|
relationship.JoinTable, association.Scope.Quote(ToSnake(relationship.AssociationForeignKey)))
|
||||||
|
|
||||||
|
scope.db.Model("").Table(relationship.JoinTable).Where(whereSql, association.PrimaryKey, addedPrimaryKeys).Delete("")
|
||||||
} else {
|
} else {
|
||||||
association.err(errors.New("replace only support many to many"))
|
association.err(errors.New("replace only support many to many"))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue