From 09ee38d6137278c0dc985e002c4cb4b50c86d61e Mon Sep 17 00:00:00 2001 From: Clark Wang Date: Thu, 12 Feb 2015 14:44:08 +0800 Subject: [PATCH] use findOrCreate for HasManyThrough#create see strongloop/loopback-connector-mongodb#92 Signed-off-by: Clark Wang --- lib/relation-definition.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/relation-definition.js b/lib/relation-definition.js index 232f232a..96267ced 100644 --- a/lib/relation-definition.js +++ b/lib/relation-definition.js @@ -954,13 +954,14 @@ HasManyThrough.prototype.create = function create(data, done) { var fk2 = keys[1]; function createRelation(to, next) { - var d = {}; - d[fk1] = modelInstance[definition.keyFrom]; - d[fk2] = to[pk2]; + var d = {}, q = {}, filter = {where:q}; + d[fk1] = q[fk1] = modelInstance[definition.keyFrom]; + d[fk2] = q[fk2] = to[pk2]; definition.applyProperties(modelInstance, d); + definition.applyScope(modelInstance, filter); // Then create the through model - modelThrough.create(d, function (e, through) { + modelThrough.findOrCreate(filter, d, function (e, through) { if (e) { // Undo creation of the target model to.destroy(function () {