diff --git a/lib/relation-definition.js b/lib/relation-definition.js index b3dfe835..09849d12 100644 --- a/lib/relation-definition.js +++ b/lib/relation-definition.js @@ -203,14 +203,15 @@ util.inherits(HasOne, Relation); * @param {*} foreignKey The foreign key * @returns {Object} The relation object */ -function findBelongsTo(model, foreignKey) { - var relations = model.relations; +function findBelongsTo(modelFrom, modelTo, keyTo) { + var relations = modelFrom.relations; var keys = Object.keys(relations); for (var k = 0; k < keys.length; k++) { var rel = relations[keys[k]]; if (rel.type === RelationTypes.belongsTo && - rel.keyFrom === foreignKey) { - return keys[k]; + rel.modelTo === modelTo && + rel.keyTo === keyTo) { + return rel.keyFrom; } } return null; @@ -378,25 +379,30 @@ HasMany.prototype.destroyById = function (id, cb) { // Create an instance of the target model and connect it to the instance of // the source model by creating an instance of the through model HasManyThrough.prototype.create = function create(data, done) { - var modelTo = this.definition.modelTo; - var modelThrough = this.definition.modelThrough; - var fk = this.definition.keyTo; - var fk2 = this.definition.keyThrough; + var definition = this.definition; + var modelTo = definition.modelTo; + var modelThrough = definition.modelThrough; if (typeof data === 'function' && !done) { done = data; data = {}; } - var self = this.modelInstance; + var modelInstance = this.modelInstance; + // First create the target model modelTo.create(data, function (err, ac) { if (err) { return done && done(err, ac); } + // The primary key for the target model + var pk2 = modelTo.dataSource.idName(modelTo.modelName) || 'id'; + var fk1 = findBelongsTo(modelThrough, definition.modelFrom, + definition.keyFrom); + var fk2 = findBelongsTo(modelThrough, definition.modelTo, pk2); var d = {}; - d[findBelongsTo(modelThrough, fk)] = self; - d[findBelongsTo(modelThrough, fk2)] = ac; + d[fk1] = modelInstance[definition.keyFrom]; + d[fk2] = ac[pk2]; // Then create the through model modelThrough.create(d, function (e) { if (e) { @@ -416,17 +422,28 @@ HasManyThrough.prototype.create = function create(data, done) { * @param {Object|ID} acInst The actual instance or id value */ HasManyThrough.prototype.add = function (acInst, done) { - var modelThrough = this.definition.modelThrough; - var fk = this.definition.keyTo; - var fk2 = this.definition.keyThrough; - var pk = this.definition.keyFrom; + var definition = this.definition; + var modelThrough = definition.modelThrough; + var modelTo = definition.modelTo; + var pk1 = definition.keyFrom; var data = {}; var query = {}; - query[fk] = this[pk]; - data[findBelongsTo(modelThrough, fk)] = this.modelInstance; - query[fk2] = acInst[pk] || acInst; - data[findBelongsTo(modelThrough, fk2)] = acInst; + + var fk1 = findBelongsTo(modelThrough, definition.modelFrom, + definition.keyFrom); + + // The primary key for the target model + var pk2 = modelTo.dataSource.idName(modelTo.modelName) || 'id'; + + var fk2 = findBelongsTo(modelThrough, definition.modelTo, pk2); + + query[fk1] = this.modelInstance[pk1]; + query[fk2] = acInst[pk2] || acInst; + + data[fk1] = this.modelInstance[pk1]; + data[fk2] = acInst[pk2] || acInst; + // Create an instance of the through model modelThrough.findOrCreate({where: query}, data, done); };