From 58596afda1bf947fd386e72493c1b4654fb69f67 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 30 Sep 2015 21:30:58 +0800 Subject: [PATCH] Replace many2many associations with empty --- association.go | 14 +++++++------- association_test.go | 5 +++++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/association.go b/association.go index 81c25f14..1773f61f 100644 --- a/association.go +++ b/association.go @@ -139,18 +139,18 @@ func (association *Association) Replace(values ...interface{}) *Association { addedPrimaryKeys = append(addedPrimaryKeys, primaryKey) } - if len(addedPrimaryKeys) > 0 { - query := scope.NewDB() - for idx, foreignKey := range relationship.ForeignDBNames { - if field, ok := scope.FieldByName(relationship.ForeignFieldNames[idx]); ok { - query = query.Where(fmt.Sprintf("%v = ?", scope.Quote(foreignKey)), field.Field.Interface()) - } + query := scope.NewDB() + for idx, foreignKey := range relationship.ForeignDBNames { + if field, ok := scope.FieldByName(relationship.ForeignFieldNames[idx]); ok { + query = query.Where(fmt.Sprintf("%v = ?", scope.Quote(foreignKey)), field.Field.Interface()) } + } + if len(addedPrimaryKeys) > 0 { sql := fmt.Sprintf("%v NOT IN (%v)", toQueryCondition(scope, relationship.AssociationForeignDBNames), toQueryMarks(addedPrimaryKeys)) query = query.Where(sql, toQueryValues(addedPrimaryKeys)...) - association.setErr(relationship.JoinTableHandler.Delete(relationship.JoinTableHandler, query, relationship)) } + association.setErr(relationship.JoinTableHandler.Delete(relationship.JoinTableHandler, query, relationship)) } else { association.setErr(errors.New("replace only support many to many")) } diff --git a/association_test.go b/association_test.go index eb606bc5..485a68e4 100644 --- a/association_test.go +++ b/association_test.go @@ -211,6 +211,11 @@ func TestManyToMany(t *testing.T) { t.Errorf("Relations should be replaced") } + DB.Model(&user).Association("Languages").Replace() + if len(user.Languages) != 0 || DB.Model(&user).Association("Languages").Count() != 0 { + t.Errorf("Relations should be replaced with empty") + } + DB.Model(&user).Association("Languages").Replace(&[]Language{{Name: "FF"}, {Name: "JJ"}}) if len(user.Languages) != 2 || DB.Model(&user).Association("Languages").Count() != len([]string{"FF", "JJ"}) { t.Errorf("Relations should be replaced")