Skip to content

Commit

Permalink
Fix association Replace delete wrong records
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Feb 9, 2015
1 parent 2c76c49 commit 8aef600
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions association.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand Down

0 comments on commit 8aef600

Please sign in to comment.