Skip to content

Commit

Permalink
Merge pull request #1402 from balderdashy/fix-add-remove
Browse files Browse the repository at this point in the history
Don't error out if the join record already exists
  • Loading branch information
particlebanana authored Oct 27, 2016
2 parents 8b46f0f + 5b0ea8b commit a14d16a
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/waterline/model/lib/associationMethods/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,14 +341,24 @@ Add.prototype.createManyToMany = function(collection, attribute, pk, key, cb) {

// First look up the record to ensure it doesn't exist
collection.findOne(criteria, function(err, val) {
if (err || val) {
return next(new Error('Trying to \'.add()\' an instance which already exists!'));
if (err) {
return next(err);
}
next();

next(null, val);
});
},

createRecord: ['validateAssociation', 'validateRecord', function(next) {
createRecord: ['validateAssociation', 'validateRecord', function(next, results) {
// If the record already exists, don't try and create it again to prevent
// duplicates.
var validateRecord = results.validateRecord;
if (validateRecord) {
return async.setImmediate(function() {
next();
});
}

collection.create(_values, next);
}]

Expand Down

0 comments on commit a14d16a

Please sign in to comment.