use findOrCreate for HasManyThrough#create

see strongloop/loopback-connector-mongodb#92

Signed-off-by: Clark Wang <clark.wangs@gmail.com>
This commit is contained in:
Clark Wang 2015-02-12 14:44:08 +08:00
parent 5d0b745f31
commit 09ee38d613
1 changed files with 5 additions and 4 deletions

View File

@ -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 () {