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

Commit c32d9367 by Mick Hansen

fix(include): fixes bug introduced in #a1926bd7

1 parent a1926bd7
...@@ -718,6 +718,7 @@ module.exports = (function() { ...@@ -718,6 +718,7 @@ module.exports = (function() {
, self = this , self = this
, query , query
, limit = options.limit , limit = options.limit
, mainModel = model
, mainQueryItems = [] , mainQueryItems = []
, mainAttributes = options.attributes && options.attributes.slice(0) , mainAttributes = options.attributes && options.attributes.slice(0)
, mainJoinQueries = [] , mainJoinQueries = []
...@@ -934,36 +935,39 @@ module.exports = (function() { ...@@ -934,36 +935,39 @@ module.exports = (function() {
} }
} else { } else {
var left = association.source var left = association.source
, right = association.target
, primaryKeysLeft = left.primaryKeyAttributes , primaryKeysLeft = left.primaryKeyAttributes
, primaryKeysRight = right.primaryKeyAttributes
, tableLeft = parentTable , tableLeft = parentTable
, attrLeft = association.associationType === 'BelongsTo' ? association.identifierField || association.identifier : primaryKeysLeft[0] , attrLeft = association.associationType === 'BelongsTo' ?
association.identifierField || association.identifier :
primaryKeysLeft[0]
, tableRight = as , 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; , joinOn;
// 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 (left.rawAttributes[attrLeft].field) {
attrLeft = left.rawAttributes[attrLeft].field;
}
}
// Filter statement // Filter statement
// Used by both join and subquery where // Used by both join and subquery where
joinOn =
// Left side if (subQuery && !include.subQuery && include.parent.subQuery && (include.hasParentRequired || include.hasParentWhere)) {
( joinOn = self.quoteIdentifier(tableLeft + '.' + attrLeft);
(subQuery && !include.subQuery && include.parent.subQuery && (include.hasParentRequired || include.hasParentWhere)) && self.quoteIdentifier(tableLeft + '.' + attrLeft) || } else {
self.quoteTable(tableLeft) + '.' + self.quoteIdentifier(attrLeft) 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 || (subQuery && !include.subQuery && include.parent.model !== mainModel)) {
if (left.rawAttributes[attrLeft].field) {
// Right side attrLeft = left.rawAttributes[attrLeft].field;
( }
self.quoteTable(tableRight) + '.' + self.quoteIdentifier(attrRight) }
); }
joinOn = self.quoteTable(tableLeft) + '.' + self.quoteIdentifier(attrLeft);
}
joinOn += ' = ' + self.quoteTable(tableRight) + '.' + self.quoteIdentifier(attrRight);
if (include.where) { if (include.where) {
joinOn += ' AND ' + self.getWhereConditions(include.where, self.sequelize.literal(self.quoteIdentifier(as)), include.model, whereOptions); joinOn += ' AND ' + self.getWhereConditions(include.where, self.sequelize.literal(self.quoteIdentifier(as)), include.model, whereOptions);
......
...@@ -1789,6 +1789,8 @@ module.exports = (function() { ...@@ -1789,6 +1789,8 @@ module.exports = (function() {
options.hasSingleAssociation = false; options.hasSingleAssociation = false;
options.hasMultiAssociation = false; options.hasMultiAssociation = false;
if (!options.model) options.model = this;
// validate all included elements // validate all included elements
var includes = options.include; var includes = options.include;
for (var index = 0; index < includes.length; index++) { 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!