Merge pull request #443 from clarkorz/fix/findOrCreate-ThroughModel

Always use findOrCreate when creating a through model
This commit is contained in:
Raymond Feng 2015-03-24 09:56:07 -07:00
commit 5e0451eef0
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]; var fk2 = keys[1];
function createRelation(to, next) { function createRelation(to, next) {
var d = {}; var d = {}, q = {}, filter = {where:q};
d[fk1] = modelInstance[definition.keyFrom]; d[fk1] = q[fk1] = modelInstance[definition.keyFrom];
d[fk2] = to[pk2]; d[fk2] = q[fk2] = to[pk2];
definition.applyProperties(modelInstance, d); definition.applyProperties(modelInstance, d);
definition.applyScope(modelInstance, filter);
// Then create the through model // Then create the through model
modelThrough.create(d, function (e, through) { modelThrough.findOrCreate(filter, d, function (e, through) {
if (e) { if (e) {
// Undo creation of the target model // Undo creation of the target model
to.destroy(function () { to.destroy(function () {