Skip to content

Commit

Permalink
don't error out if the join record already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
particlebanana committed Oct 27, 2016
1 parent 144f80a commit 5b0ea8b
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 @@ -340,14 +340,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 5b0ea8b

Please sign in to comment.