From 7e8622f67140131e4afdc0cb54acb5acfff71943 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 18 Jun 2015 18:23:11 +0800 Subject: [PATCH] Don't need to delete join table records if no record added --- association.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/association.go b/association.go index 37e10516..dbc928e8 100644 --- a/association.go +++ b/association.go @@ -131,9 +131,11 @@ func (association *Association) Replace(values ...interface{}) *Association { addedPrimaryKeys = append(addedPrimaryKeys, primaryKey) } - sql := fmt.Sprintf("%v = ? AND %v NOT IN (?)", scope.Quote(relationship.ForeignDBName), scope.Quote(relationship.AssociationForeignDBName)) - query := scope.NewDB().Where(sql, association.PrimaryKey, addedPrimaryKeys) - association.setErr(relationship.JoinTableHandler.Delete(query, relationship)) + if len(addedPrimaryKeys) > 0 { + sql := fmt.Sprintf("%v = ? AND %v NOT IN (?)", scope.Quote(relationship.ForeignDBName), scope.Quote(relationship.AssociationForeignDBName)) + query := scope.NewDB().Where(sql, association.PrimaryKey, addedPrimaryKeys) + association.setErr(relationship.JoinTableHandler.Delete(query, relationship)) + } } else { association.setErr(errors.New("replace only support many to many")) }