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

Commit c32d9367 by Mick Hansen

fix(include): fixes bug introduced in #a1926bd7

1 parent a1926bd7
......@@ -718,6 +718,7 @@ module.exports = (function() {
, self = this
, query
, limit = options.limit
, mainModel = model
, mainQueryItems = []
, mainAttributes = options.attributes && options.attributes.slice(0)
, mainJoinQueries = []
......@@ -934,36 +935,39 @@ module.exports = (function() {
}
} else {
var left = association.source
, right = association.target
, primaryKeysLeft = left.primaryKeyAttributes
, primaryKeysRight = right.primaryKeyAttributes
, tableLeft = parentTable
, attrLeft = association.associationType === 'BelongsTo' ? association.identifierField || association.identifier : primaryKeysLeft[0]
, attrLeft = association.associationType === 'BelongsTo' ?
association.identifierField || association.identifier :
primaryKeysLeft[0]
, tableRight = as
, attrRight = association.associationType !== 'BelongsTo' ? association.identifierField || association.identifier : primaryKeysLeft[0]
, attrRight = association.associationType !== 'BelongsTo' ?
association.identifierField || association.identifier :
right.rawAttributes[primaryKeysRight[0]].field || primaryKeysRight[0]
, joinOn;
// Filter statement
// Used by both join and subquery where
if (subQuery && !include.subQuery && include.parent.subQuery && (include.hasParentRequired || include.hasParentWhere)) {
joinOn = self.quoteIdentifier(tableLeft + '.' + attrLeft);
} else {
if (association.associationType !== 'BelongsTo') {
// Alias the left attribute if the left attribute is not from a subqueried main table
// When doing a query like SELECT aliasedKey FROM (SELECT primaryKey FROM primaryTable) only aliasedKey is available to the join, this is not the case when doing a regular select where you can't used the aliased attribute
if (!subQuery || parentTable !== mainTableAs || tableLeft !== parentTable) {
if (!subQuery || (subQuery && !include.subQuery && include.parent.model !== mainModel)) {
if (left.rawAttributes[attrLeft].field) {
attrLeft = left.rawAttributes[attrLeft].field;
}
}
}
joinOn = self.quoteTable(tableLeft) + '.' + self.quoteIdentifier(attrLeft);
}
// Filter statement
// Used by both join and subquery where
joinOn =
// Left side
(
(subQuery && !include.subQuery && include.parent.subQuery && (include.hasParentRequired || include.hasParentWhere)) && self.quoteIdentifier(tableLeft + '.' + attrLeft) ||
self.quoteTable(tableLeft) + '.' + self.quoteIdentifier(attrLeft)
)
+ ' = ' +
// Right side
(
self.quoteTable(tableRight) + '.' + self.quoteIdentifier(attrRight)
);
joinOn += ' = ' + self.quoteTable(tableRight) + '.' + self.quoteIdentifier(attrRight);
if (include.where) {
joinOn += ' AND ' + self.getWhereConditions(include.where, self.sequelize.literal(self.quoteIdentifier(as)), include.model, whereOptions);
......
......@@ -1789,6 +1789,8 @@ module.exports = (function() {
options.hasSingleAssociation = false;
options.hasMultiAssociation = false;
if (!options.model) options.model = this;
// validate all included elements
var includes = options.include;
for (var index = 0; index < includes.length; index++) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!