From 8aef60054028a63e89e78f3e1f813226e8d274e6 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Mon, 9 Feb 2015 21:54:16 +0800 Subject: [PATCH] Fix association Replace delete wrong records --- association.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/association.go b/association.go index bb692163..4103854a 100644 --- a/association.go +++ b/association.go @@ -137,8 +137,11 @@ func (association *Association) Replace(values ...interface{}) *Association { addedPrimaryKeys = append(addedPrimaryKeys, primaryKey) } - whereSql := fmt.Sprintf("%v.%v NOT IN (?)", relationship.JoinTable, scope.Quote(ToSnake(relationship.AssociationForeignKey))) - scope.db.Model("").Table(relationship.JoinTable).Where(whereSql, addedPrimaryKeys).Delete("") + whereSql := fmt.Sprintf("%v.%v = ? AND %v.%v NOT IN (?)", + 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 { association.err(errors.New("replace only support many to many")) }