不要怂,就是干,撸起袖子干!

Commit e3e4b31f by Mick Hansen

chore(associations): through -> through.model

1 parent 826d0407
...@@ -30,11 +30,11 @@ module.exports = (function() { ...@@ -30,11 +30,11 @@ module.exports = (function() {
options.where = new Utils.and([ options.where = new Utils.and([
new Utils.where( new Utils.where(
through.rawAttributes[self.association.identifier], through.model.rawAttributes[self.association.identifier],
self.instance[instancePrimaryKey] self.instance[instancePrimaryKey]
), ),
new Utils.where( new Utils.where(
through.rawAttributes[self.association.foreignIdentifier], through.model.rawAttributes[self.association.foreignIdentifier],
{ {
join: new Utils.literal([ join: new Utils.literal([
self.QueryInterface.quoteTable(self.association.target.name), self.QueryInterface.quoteTable(self.association.target.name),
...@@ -46,9 +46,9 @@ module.exports = (function() { ...@@ -46,9 +46,9 @@ module.exports = (function() {
options.where options.where
]); ]);
if (Object(targetAssociation.through) === targetAssociation.through) { if (Object(targetAssociation.through.model) === targetAssociation.through.model) {
queryOptions.hasJoinTableModel = true; queryOptions.hasJoinTableModel = true;
queryOptions.joinTableModel = through; queryOptions.joinTableModel = through.model;
if (!options.attributes) { if (!options.attributes) {
options.attributes = [ options.attributes = [
...@@ -59,21 +59,21 @@ module.exports = (function() { ...@@ -59,21 +59,21 @@ module.exports = (function() {
if (options.joinTableAttributes) { if (options.joinTableAttributes) {
options.joinTableAttributes.forEach(function(elem) { options.joinTableAttributes.forEach(function(elem) {
options.attributes.push( options.attributes.push(
self.QueryInterface.quoteTable(through.name) + '.' + self.QueryInterface.quoteIdentifier(elem) + ' as ' + self.QueryInterface.quoteTable(through.model.name) + '.' + self.QueryInterface.quoteIdentifier(elem) + ' as ' +
self.QueryInterface.quoteIdentifier(through.name + '.' + elem, true) self.QueryInterface.quoteIdentifier(through.model.name + '.' + elem, true)
); );
}); });
} else { } else {
Utils._.forOwn(through.rawAttributes, function(elem, key) { Utils._.forOwn(through.model.rawAttributes, function(elem, key) {
options.attributes.push( options.attributes.push(
self.QueryInterface.quoteTable(through.name) + '.' + self.QueryInterface.quoteIdentifier(key) + ' as ' + self.QueryInterface.quoteTable(through.model.name) + '.' + self.QueryInterface.quoteIdentifier(key) + ' as ' +
self.QueryInterface.quoteIdentifier(through.name + '.' + key, true) self.QueryInterface.quoteIdentifier(through.model.name + '.' + key, true)
); );
}); });
} }
} }
return self.association.target.findAllJoin([through.getTableName(), through.name], options, queryOptions); return self.association.target.findAllJoin([through.model.getTableName(), through.model.name], options, queryOptions);
}; };
HasManyDoubleLinked.prototype.injectSetter = function(oldAssociations, newAssociations, defaultAttributes) { HasManyDoubleLinked.prototype.injectSetter = function(oldAssociations, newAssociations, defaultAttributes) {
...@@ -105,10 +105,10 @@ module.exports = (function() { ...@@ -105,10 +105,10 @@ module.exports = (function() {
if (!newObj) { if (!newObj) {
obsoleteAssociations.push(old); obsoleteAssociations.push(old);
} else if (Object(targetAssociation.through) === targetAssociation.through) { } else if (Object(targetAssociation.through.model) === targetAssociation.through.model) {
var throughAttributes = newObj[self.association.through.name]; var throughAttributes = newObj[self.association.through.model.name];
// Quick-fix for subtle bug when using existing objects that might have the through model attached (not as an attribute object) // Quick-fix for subtle bug when using existing objects that might have the through model attached (not as an attribute object)
if (throughAttributes instanceof self.association.through.DAO) { if (throughAttributes instanceof self.association.through.model.Instance) {
throughAttributes = {}; throughAttributes = {};
} }
...@@ -136,7 +136,7 @@ module.exports = (function() { ...@@ -136,7 +136,7 @@ module.exports = (function() {
where[self.association.identifier] = ((sourceKeys.length === 1) ? self.instance[sourceKeys[0]] : self.instance.id); where[self.association.identifier] = ((sourceKeys.length === 1) ? self.instance[sourceKeys[0]] : self.instance.id);
where[foreignIdentifier] = foreignIds; where[foreignIdentifier] = foreignIds;
promises.push(self.association.through.destroy(where, options)); promises.push(self.association.through.model.destroy(where, options));
} }
if (unassociatedObjects.length > 0) { if (unassociatedObjects.length > 0) {
...@@ -146,19 +146,19 @@ module.exports = (function() { ...@@ -146,19 +146,19 @@ module.exports = (function() {
attributes[self.association.identifier] = ((sourceKeys.length === 1) ? self.instance[sourceKeys[0]] : self.instance.id); attributes[self.association.identifier] = ((sourceKeys.length === 1) ? self.instance[sourceKeys[0]] : self.instance.id);
attributes[foreignIdentifier] = ((targetKeys.length === 1) ? unassociatedObject[targetKeys[0]] : unassociatedObject.id); attributes[foreignIdentifier] = ((targetKeys.length === 1) ? unassociatedObject[targetKeys[0]] : unassociatedObject.id);
if (Object(targetAssociation.through) === targetAssociation.through) { if (Object(targetAssociation.through.model) === targetAssociation.through.model) {
attributes = Utils._.defaults(attributes, unassociatedObject[targetAssociation.through.name], defaultAttributes); attributes = Utils._.defaults(attributes, unassociatedObject[targetAssociation.through.model.name], defaultAttributes);
} }
return attributes; return attributes;
}); });
promises.push(self.association.through.bulkCreate(bulk, options)); promises.push(self.association.through.model.bulkCreate(bulk, options));
} }
if (changedAssociations.length > 0) { if (changedAssociations.length > 0) {
changedAssociations.forEach(function(assoc) { changedAssociations.forEach(function(assoc) {
promises.push(self.association.through.update(assoc.attributes, assoc.where, options)); promises.push(self.association.through.model.update(assoc.attributes, assoc.where, options));
}); });
} }
...@@ -183,17 +183,17 @@ module.exports = (function() { ...@@ -183,17 +183,17 @@ module.exports = (function() {
if (exists) { if (exists) {
var where = attributes; var where = attributes;
attributes = Utils._.defaults({}, newAssociation[targetAssociation.through.name], additionalAttributes); attributes = Utils._.defaults({}, newAssociation[targetAssociation.through.model.name], additionalAttributes);
if (Object.keys(attributes).length) { if (Object.keys(attributes).length) {
return targetAssociation.through.update(attributes, where, options); return targetAssociation.through.model.update(attributes, where, options);
} else { } else {
return Utils.Promise.resolve(); return Utils.Promise.resolve();
} }
} else { } else {
attributes = Utils._.defaults(attributes, newAssociation[targetAssociation.through.name], additionalAttributes); attributes = Utils._.defaults(attributes, newAssociation[targetAssociation.through.model.name], additionalAttributes);
return this.association.through.create(attributes, options); return this.association.through.model.create(attributes, options);
} }
}; };
......
...@@ -4,7 +4,8 @@ var Utils = require('./../utils') ...@@ -4,7 +4,8 @@ var Utils = require('./../utils')
, Helpers = require('./helpers') , Helpers = require('./helpers')
, _ = require('lodash') , _ = require('lodash')
, Association = require('./base') , Association = require('./base')
, Transaction = require('../transaction'); , Transaction = require('../transaction')
, Model = require('../model');
var HasManySingleLinked = require('./has-many-single-linked') var HasManySingleLinked = require('./has-many-single-linked')
, HasManyDoubleLinked = require('./has-many-double-linked'); , HasManyDoubleLinked = require('./has-many-double-linked');
...@@ -57,16 +58,22 @@ module.exports = (function() { ...@@ -57,16 +58,22 @@ module.exports = (function() {
} }
} }
if (this.through !== null && !this.through.model) {
this.through = {
model: this.through
};
}
/* /*
* Determine associationAccessor, especially for include options to identify the correct model * Determine associationAccessor, especially for include options to identify the correct model
*/ */
this.associationAccessor = this.as; this.associationAccessor = this.as;
if (!this.associationAccessor) { if (!this.associationAccessor) {
if (typeof this.through === 'string') { if (typeof this.through.model === 'string') {
this.associationAccessor = this.through; this.associationAccessor = this.through.model;
} else if (Object(this.through) === this.through) { } else if (Object(this.through.model) === this.through.model) {
this.associationAccessor = this.through.tableName; this.associationAccessor = this.through.model.tableName;
} else { } else {
this.associationAccessor = this.combinedTableName; this.associationAccessor = this.combinedTableName;
} }
...@@ -77,7 +84,7 @@ module.exports = (function() { ...@@ -77,7 +84,7 @@ module.exports = (function() {
*/ */
if (this.isSelfAssociation) { if (this.isSelfAssociation) {
// check 'as' is defined for many-to-many self-association // check 'as' is defined for many-to-many self-association
if (this.through && this.through !== true && !this.as) { if (this.through && this.through.model !== true && !this.as) {
throw new Error('\'as\' must be defined for many-to-many self-associations'); throw new Error('\'as\' must be defined for many-to-many self-associations');
} }
...@@ -93,12 +100,15 @@ module.exports = (function() { ...@@ -93,12 +100,15 @@ module.exports = (function() {
var paired; var paired;
// If through is default, we determine pairing by the accesor value (i.e. DAOFactory's using as won't pair, but regular ones will) // If through is default, we determine pairing by the accesor value (i.e. DAOFactory's using as won't pair, but regular ones will)
if (self.through === true) { if (self.through.model === true) {
paired = accessor === self.associationAccessor; paired = accessor === self.associationAccessor;
} }
// If through is not default, determine pairing by through value (model/string) // If through is not default, determine pairing by through value (model/string)
else { else {
paired = self.options.through === association.options.through; paired = self.options.through === association.options.through ||
self.options.through === association.options.through.model ||
self.options.through.model === association.options.through.model ||
self.options.through.model === association.options.through;
} }
// If paired, set properties identifying both associations as double linked, and allow them to each eachtoerh // If paired, set properties identifying both associations as double linked, and allow them to each eachtoerh
if (paired) { if (paired) {
...@@ -115,13 +125,13 @@ module.exports = (function() { ...@@ -115,13 +125,13 @@ module.exports = (function() {
/* /*
* If we are double linked, and through is either default or a string, we create the through model and set it on both associations * If we are double linked, and through is either default or a string, we create the through model and set it on both associations
*/ */
if (this.doubleLinked && this.through === true) { if (this.doubleLinked && this.through.model === true) {
this.through = this.combinedTableName; this.through.model = this.combinedTableName;
} }
if (typeof this.through === 'string') { if (typeof this.through.model === 'string') {
this.through = this.sequelize.define(this.through, {}, _.extend(this.options, { this.through.model = this.sequelize.define(this.through.model, {}, _.extend(this.options, {
tableName: this.through, tableName: this.through.model,
paranoid: false // A paranoid join table does not make sense paranoid: false // A paranoid join table does not make sense
})); }));
...@@ -130,7 +140,11 @@ module.exports = (function() { ...@@ -130,7 +140,11 @@ module.exports = (function() {
} }
} }
this.options.tableName = this.combinedName = (this.through === Object(this.through) ? this.through.tableName : this.through); if (this.through) {
this.throughModel = this.through.model;
}
this.options.tableName = this.combinedName = (this.through.model === Object(this.through.model) ? this.through.model.tableName : this.through.model);
if (this.as) { if (this.as) {
this.isAliased = true; this.isAliased = true;
...@@ -181,16 +195,16 @@ module.exports = (function() { ...@@ -181,16 +195,16 @@ module.exports = (function() {
// is there already a single sided association between the source and the target? // is there already a single sided association between the source and the target?
// or is the association on the model itself? // or is the association on the model itself?
if ((this.isSelfAssociation && Object(this.through) === this.through) || doubleLinked) { if ((this.isSelfAssociation && Object(this.through.model) === this.through.model) || doubleLinked) {
// We need to remove the keys that 1:M have added // We need to remove the keys that 1:M have added
if (this.isSelfAssociation && doubleLinked) { if (this.isSelfAssociation && doubleLinked) {
if (self.through.rawAttributes[this.targetAssociation.identifier] if (self.through.model.rawAttributes[this.targetAssociation.identifier]
&& self.through.rawAttributes[this.targetAssociation.identifier]._autoGenerated) { && self.through.model.rawAttributes[this.targetAssociation.identifier]._autoGenerated) {
delete self.through.rawAttributes[this.targetAssociation.identifier]; delete self.through.model.rawAttributes[this.targetAssociation.identifier];
} }
if (self.through.rawAttributes[this.targetAssociation.foreignIdentifier] if (self.through.model.rawAttributes[this.targetAssociation.foreignIdentifier]
&& self.through.rawAttributes[this.targetAssociation.foreignIdentifier]._autoGenerated) { && self.through.model.rawAttributes[this.targetAssociation.foreignIdentifier]._autoGenerated) {
delete self.through.rawAttributes[this.targetAssociation.foreignIdentifier]; delete self.through.model.rawAttributes[this.targetAssociation.foreignIdentifier];
} }
} }
...@@ -213,9 +227,9 @@ module.exports = (function() { ...@@ -213,9 +227,9 @@ module.exports = (function() {
} }
// remove any PKs previously defined by sequelize // remove any PKs previously defined by sequelize
Utils._.each(this.through.rawAttributes, function(attribute, attributeName) { Utils._.each(this.through.model.rawAttributes, function(attribute, attributeName) {
if (attribute.primaryKey === true && attribute._autoGenerated === true) { if (attribute.primaryKey === true && attribute._autoGenerated === true) {
delete self.through.rawAttributes[attributeName]; delete self.through.model.rawAttributes[attributeName];
self.targetAssociation.primaryKeyDeleted = true; self.targetAssociation.primaryKeyDeleted = true;
} }
}); });
...@@ -245,27 +259,27 @@ module.exports = (function() { ...@@ -245,27 +259,27 @@ module.exports = (function() {
if (this.targetAssociation.primaryKeyDeleted === true) { if (this.targetAssociation.primaryKeyDeleted === true) {
targetAttribute.primaryKey = sourceAttribute.primaryKey = true; targetAttribute.primaryKey = sourceAttribute.primaryKey = true;
} else { } else if (this.through.unique !== false) {
var uniqueKey = [this.through.tableName, this.identifier, this.foreignIdentifier, 'unique'].join('_'); var uniqueKey = [this.through.model.tableName, this.identifier, this.foreignIdentifier, 'unique'].join('_');
targetAttribute.unique = sourceAttribute.unique = uniqueKey; targetAttribute.unique = sourceAttribute.unique = uniqueKey;
} }
if (!this.through.rawAttributes[this.identifier]) { if (!this.through.model.rawAttributes[this.identifier]) {
this.through.rawAttributes[this.identifier] = { this.through.model.rawAttributes[this.identifier] = {
_autoGenerated: true _autoGenerated: true
}; };
} }
if (!this.through.rawAttributes[this.foreignIdentifier]) { if (!this.through.model.rawAttributes[this.foreignIdentifier]) {
this.through.rawAttributes[this.foreignIdentifier] = { this.through.model.rawAttributes[this.foreignIdentifier] = {
_autoGenerated: true _autoGenerated: true
}; };
} }
this.through.rawAttributes[this.identifier] = Utils._.extend(this.through.rawAttributes[this.identifier], sourceAttribute); this.through.model.rawAttributes[this.identifier] = Utils._.extend(this.through.model.rawAttributes[this.identifier], sourceAttribute);
this.through.rawAttributes[this.foreignIdentifier] = Utils._.extend(this.through.rawAttributes[this.foreignIdentifier], targetAttribute); this.through.model.rawAttributes[this.foreignIdentifier] = Utils._.extend(this.through.model.rawAttributes[this.foreignIdentifier], targetAttribute);
this.through.init(this.through.daoFactoryManager); this.through.model.init(this.through.model.daoFactoryManager);
} else { } else {
var newAttributes = {}; var newAttributes = {};
var constraintOptions = _.clone(this.options); // Create a new options object for use with addForeignKeyConstraints, to avoid polluting this.options in case it is later used for a n:m var constraintOptions = _.clone(this.options); // Create a new options object for use with addForeignKeyConstraints, to avoid polluting this.options in case it is later used for a n:m
...@@ -294,7 +308,7 @@ module.exports = (function() { ...@@ -294,7 +308,7 @@ module.exports = (function() {
obj[this.accessors.get] = function(options, queryOptions) { obj[this.accessors.get] = function(options, queryOptions) {
options = options || {}; options = options || {};
queryOptions = queryOptions || {}; queryOptions = queryOptions || {};
var Class = Object(association.through) === association.through ? HasManyDoubleLinked : HasManySingleLinked; var Class = Object(association.through.model) === association.through.model ? HasManyDoubleLinked : HasManySingleLinked;
return new Class(association, this).injectGetter(options, queryOptions); return new Class(association, this).injectGetter(options, queryOptions);
}; };
...@@ -380,7 +394,7 @@ module.exports = (function() { ...@@ -380,7 +394,7 @@ module.exports = (function() {
return instance[association.accessors.get]({}, { return instance[association.accessors.get]({}, {
transaction: (additionalAttributes || {}).transaction transaction: (additionalAttributes || {}).transaction
}).then(function(oldAssociatedObjects) { }).then(function(oldAssociatedObjects) {
var Class = Object(association.through) === association.through ? HasManyDoubleLinked : HasManySingleLinked; var Class = Object(association.through.model) === association.through.model ? HasManyDoubleLinked : HasManySingleLinked;
return new Class(association, instance).injectSetter(oldAssociatedObjects, newAssociatedObjects, additionalAttributes); return new Class(association, instance).injectSetter(oldAssociatedObjects, newAssociatedObjects, additionalAttributes);
}); });
}; };
...@@ -404,7 +418,7 @@ module.exports = (function() { ...@@ -404,7 +418,7 @@ module.exports = (function() {
return newInstance; return newInstance;
}); });
var Class = Object(association.through) === association.through ? HasManyDoubleLinked : HasManySingleLinked; var Class = Object(association.through.model) === association.through.model ? HasManyDoubleLinked : HasManySingleLinked;
return new Class(association, this).injectSetter([], newInstances, additionalAttributes); return new Class(association, this).injectSetter([], newInstances, additionalAttributes);
} else { } else {
if (!(newInstance instanceof association.target.Instance)) { if (!(newInstance instanceof association.target.Instance)) {
...@@ -420,8 +434,8 @@ module.exports = (function() { ...@@ -420,8 +434,8 @@ module.exports = (function() {
}, { }, {
transaction: (additionalAttributes || {}).transaction transaction: (additionalAttributes || {}).transaction
}).then(function(currentAssociatedObjects) { }).then(function(currentAssociatedObjects) {
if (currentAssociatedObjects.length === 0 || Object(association.through) === association.through) { if (currentAssociatedObjects.length === 0 || Object(association.through.model) === association.through.model) {
var Class = Object(association.through) === association.through ? HasManyDoubleLinked : HasManySingleLinked; var Class = Object(association.through.model) === association.through.model ? HasManyDoubleLinked : HasManySingleLinked;
return new Class(association, instance).injectAdder(newInstance, additionalAttributes, !!currentAssociatedObjects.length); return new Class(association, instance).injectAdder(newInstance, additionalAttributes, !!currentAssociatedObjects.length);
} else { } else {
return Utils.Promise.resolve(currentAssociatedObjects[0]); return Utils.Promise.resolve(currentAssociatedObjects[0]);
...@@ -464,7 +478,7 @@ module.exports = (function() { ...@@ -464,7 +478,7 @@ module.exports = (function() {
}); });
} }
if (Object(association.through) === association.through) { if (Object(association.through.model) === association.through.model) {
// Create the related model instance // Create the related model instance
return association.target.create(values, fieldsOrOptions).then(function(newAssociatedObject) { return association.target.create(values, fieldsOrOptions).then(function(newAssociatedObject) {
return instance[association.accessors.add](newAssociatedObject, fieldsOrOptions).return(newAssociatedObject); return instance[association.accessors.add](newAssociatedObject, fieldsOrOptions).return(newAssociatedObject);
......
...@@ -230,6 +230,7 @@ Mixin.belongsTo = singleLinked(BelongsTo); ...@@ -230,6 +230,7 @@ Mixin.belongsTo = singleLinked(BelongsTo);
* @param {Model|string|object} [options.through] The name of the table that is used to join source and target in n:m associations. Can also be a sequelize model if you want to define the junction table yourself and add extra attributes to it. * @param {Model|string|object} [options.through] The name of the table that is used to join source and target in n:m associations. Can also be a sequelize model if you want to define the junction table yourself and add extra attributes to it.
* @param {Model} [options.through.model] The model used to join both sides of the N:M association. * @param {Model} [options.through.model] The model used to join both sides of the N:M association.
* @param {object} [options.through.scope] A key/value set that will be used for association create and find defaults on the through model. (Remember to add the attributes to the through model) * @param {object} [options.through.scope] A key/value set that will be used for association create and find defaults on the through model. (Remember to add the attributes to the through model)
* @param {boolean} [options.through.unique=true] If true a unique key will be generated from the foreign keys used (might want to turn this off and create specific unique keys when using scopes)
* @param {string|object} [options.as] The alias of this model. If you provide a string, it should be plural, and will be singularized using node.inflection. If you want to control the singular version yourself, provide an object with `plural` and `singular` keys. See also the `name` option passed to `sequelize.define`. If you create multiple associations between the same tables, you should provide an alias to be able to distinguish between them. If you provide an alias when creating the assocition, you should provide the same alias when eager loading and when getting assocated models. Defaults to the pluralized name of target * @param {string|object} [options.as] The alias of this model. If you provide a string, it should be plural, and will be singularized using node.inflection. If you want to control the singular version yourself, provide an object with `plural` and `singular` keys. See also the `name` option passed to `sequelize.define`. If you create multiple associations between the same tables, you should provide an alias to be able to distinguish between them. If you provide an alias when creating the assocition, you should provide the same alias when eager loading and when getting assocated models. Defaults to the pluralized name of target
* @param {string|object} [options.foreignKey] The name of the foreign key in the target table / join table or an object representing the type definition for the foreign column (see `Sequelize.define` for syntax). When using an object, you can add a `name` property to set the name of the colum. Defaults to the name of source + primary key of source * @param {string|object} [options.foreignKey] The name of the foreign key in the target table / join table or an object representing the type definition for the foreign column (see `Sequelize.define` for syntax). When using an object, you can add a `name` property to set the name of the colum. Defaults to the name of source + primary key of source
* @param {object} [options.scope] A key/value set that will be used for association create and find defaults on the target. (sqlite not supported for N:M) * @param {object} [options.scope] A key/value set that will be used for association create and find defaults on the target. (sqlite not supported for N:M)
......
...@@ -886,7 +886,6 @@ module.exports = (function() { ...@@ -886,7 +886,6 @@ module.exports = (function() {
joinQueryItem += joinType + self.quoteTable(throughTable, throughAs) + ' ON '; joinQueryItem += joinType + self.quoteTable(throughTable, throughAs) + ' ON ';
joinQueryItem += sourceJoinOn; joinQueryItem += sourceJoinOn;
// Generate join SQL for right side of through // Generate join SQL for right side of through
joinQueryItem += joinType + self.quoteTable(table, as) + ' ON '; joinQueryItem += joinType + self.quoteTable(table, as) + ' ON ';
joinQueryItem += targetJoinOn; joinQueryItem += targetJoinOn;
...@@ -1405,14 +1404,14 @@ module.exports = (function() { ...@@ -1405,14 +1404,14 @@ module.exports = (function() {
joins += ' LEFT JOIN ' + self.quoteIdentifiers(association.target.tableName); joins += ' LEFT JOIN ' + self.quoteIdentifiers(association.target.tableName);
joins += ' ON ' + self.quoteIdentifiers(association.source.tableName + '.' + association.identifier); joins += ' ON ' + self.quoteIdentifiers(association.source.tableName + '.' + association.identifier);
joins += ' = ' + self.quoteIdentifiers(association.target.tableName + '.' + association.target.autoIncrementField); joins += ' = ' + self.quoteIdentifiers(association.target.tableName + '.' + association.target.autoIncrementField);
} else if (Object(association.through) === association.through) { } else if (Object(association.through.model) === association.through.model) {
joinedTables[association.through.tableName] = true; joinedTables[association.through.model.tableName] = true;
joins += ' LEFT JOIN ' + self.quoteIdentifiers(association.through.tableName); joins += ' LEFT JOIN ' + self.quoteIdentifiers(association.through.model.tableName);
joins += ' ON ' + self.quoteIdentifiers(association.source.tableName + '.' + association.source.autoIncrementField); joins += ' ON ' + self.quoteIdentifiers(association.source.tableName + '.' + association.source.autoIncrementField);
joins += ' = ' + self.quoteIdentifiers(association.through.tableName + '.' + association.identifier); joins += ' = ' + self.quoteIdentifiers(association.through.model.tableName + '.' + association.identifier);
joins += ' LEFT JOIN ' + self.quoteIdentifiers(association.target.tableName); joins += ' LEFT JOIN ' + self.quoteIdentifiers(association.target.tableName);
joins += ' ON ' + self.quoteIdentifiers(association.through.tableName + '.' + association.foreignIdentifier); joins += ' ON ' + self.quoteIdentifiers(association.through.model.tableName + '.' + association.foreignIdentifier);
joins += ' = ' + self.quoteIdentifiers(association.target.tableName + '.' + association.target.autoIncrementField); joins += ' = ' + self.quoteIdentifiers(association.target.tableName + '.' + association.target.autoIncrementField);
} else { } else {
joins += ' LEFT JOIN ' + self.quoteIdentifiers(association.target.tableName); joins += ' LEFT JOIN ' + self.quoteIdentifiers(association.target.tableName);
......
...@@ -1762,13 +1762,13 @@ module.exports = (function() { ...@@ -1762,13 +1762,13 @@ module.exports = (function() {
include.as = association.as; include.as = association.as;
// If through, we create a pseudo child include, to ease our parsing later on // If through, we create a pseudo child include, to ease our parsing later on
if (Object(include.association.through) === include.association.through) { if (include.association.through && Object(include.association.through.model) === include.association.through.model) {
if (!include.include) include.include = []; if (!include.include) include.include = [];
var through = include.association.through; var through = include.association.through;
include.through = Utils._.defaults(include.through || {}, { include.through = Utils._.defaults(include.through || {}, {
model: through, model: through.model,
as: Utils.singularize(through.tableName), as: Utils.singularize(through.model.tableName),
association: { association: {
isSingleAssociation: true isSingleAssociation: true
}, },
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!