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

Commit 31e9b3f1 by Matt Broadstone

fix find with nested include

1 parent fd0d5059
Showing with 39 additions and 32 deletions
...@@ -599,11 +599,14 @@ module.exports = (function() { ...@@ -599,11 +599,14 @@ 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 = []
// We'll use a subquery if we have hasMany associations and a limit and a filtered/required association // We'll use a subquery if we have hasMany associations and a limit and a filtered/required association
, subQuery = limit && (options.hasIncludeWhere || options.hasIncludeRequired || options.hasMultiAssociation) && options.subQuery !== false , subQuery = options.subQuery === undefined ?
limit && (options.hasIncludeWhere || options.hasIncludeRequired || options.hasMultiAssociation) && options.subQuery !== false :
options.subquery
, subQueryItems = [] , subQueryItems = []
, subQueryAttributes = null , subQueryAttributes = null
, subJoinQueries = [] , subJoinQueries = []
...@@ -813,41 +816,45 @@ module.exports = (function() { ...@@ -813,41 +816,45 @@ module.exports = (function() {
} }
} }
} else { } else {
var left = association.associationType === 'BelongsTo' ? association.target : association.source var left = association.source
, right = association.target
, primaryKeysLeft = left.primaryKeyAttributes , primaryKeysLeft = left.primaryKeyAttributes
, tableLeft = association.associationType === 'BelongsTo' ? as : parentTable , primaryKeysRight = right.primaryKeyAttributes
, attrLeft = primaryKeysLeft[0] , tableLeft = parentTable
, tableRight = association.associationType === 'BelongsTo' ? parentTable : as , attrLeft = association.associationType === 'BelongsTo' ?
, attrRight = association.identifierField || association.identifier association.identifierField || association.identifier :
primaryKeysLeft[0]
, tableRight = as
, 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
(
(subQuery && !include.subQuery && include.parent.subQuery && !(include.hasParentRequired && include.hasParentWhere)) && SqlGenerator.quoteIdentifier(tableLeft + '.' + attrLeft) ||
SqlGenerator.quoteIdentifier(tableLeft) + '.' + SqlGenerator.quoteIdentifier(attrLeft)
)
+ ' = ' + 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 || (subQuery && !include.subQuery && include.parent.model !== mainModel)) {
if (left.rawAttributes[attrLeft].field) {
attrLeft = left.rawAttributes[attrLeft].field;
}
}
}
joinOn = self.quoteTable(tableLeft) + '.' + self.quoteIdentifier(attrLeft);
}
// Right side joinOn += ' = ' + self.quoteTable(tableRight) + '.' + self.quoteIdentifier(attrRight);
(
(subQuery && !include.subQuery && include.parent.subQuery && (include.hasParentRequired && include.hasParentWhere)) && SqlGenerator.quoteIdentifier(tableRight + '.' + attrRight) ||
SqlGenerator.quoteIdentifier(tableRight) + '.' + SqlGenerator.quoteIdentifier(attrRight)
);
if (include.where) { if (include.where) {
joinOn += ' AND ' + self.getWhereConditions(include.where, self.sequelize.literal(SqlGenerator.quoteIdentifier(as)), include.model, whereOptions); joinOn += ' AND ' + self.getWhereConditions(include.where, self.sequelize.literal(self.quoteIdentifier(as)), include.model, whereOptions);
// If its a multi association we need to add a where query to the main where (executed in the subquery) // If its a multi association we need to add a where query to the main where (executed in the subquery)
if (subQuery && association.isMultiAssociation && include.required) { if (subQuery && association.isMultiAssociation && include.required) {
...@@ -856,17 +863,17 @@ module.exports = (function() { ...@@ -856,17 +863,17 @@ module.exports = (function() {
// Creating the as-is where for the subQuery, checks that the required association exists // Creating the as-is where for the subQuery, checks that the required association exists
options.where['__' + as] = self.sequelize.asIs(['(', options.where['__' + as] = self.sequelize.asIs(['(',
'SELECT TOP(1)' + SqlGenerator.quoteIdentifier(attrRight), 'SELECT TOP(1)' + self.quoteIdentifier(attrRight),
'FROM ' + SqlGenerator.quoteTable(table, as), 'FROM ' + self.quoteTable(table, as),
'WHERE ' + joinOn, 'WHERE ' + joinOn,
')', 'IS NOT NULL'].join(' ')); ')', 'IS NOT NULL'].join(' '));
} }
} }
// Generate join SQL // Generate join SQL
joinQueryItem += joinType + SqlGenerator.quoteTable(table,as) + ' ON ' + joinOn; joinQueryItem += joinType + self.quoteTable(table, as) + ' ON ' + joinOn;
} }
if (include.subQuery && subQuery) { if (include.subQuery && subQuery) {
joinQueries.subQuery.push(joinQueryItem); joinQueries.subQuery.push(joinQueryItem);
} else { } else {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!