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

Commit b2e57f18 by José Moreira

SubQuery 1:M fix

related with #1713
1 parent 596f7134
Showing with 13 additions and 6 deletions
...@@ -765,11 +765,11 @@ module.exports = (function() { ...@@ -765,11 +765,11 @@ module.exports = (function() {
, attrLeft = primaryKeysLeft[0] , attrLeft = primaryKeysLeft[0]
, tableRight = association.associationType === 'BelongsTo' ? parentTable : as , tableRight = association.associationType === 'BelongsTo' ? parentTable : as
, attrRight = association.identifier , attrRight = association.identifier
, where , joinOn
// Filter statement // Filter statement
// Used by both join and subquery where // Used by both join and subquery where
where = joinOn =
// Left side // Left side
( (
( subQuery && !include.subQuery && include.parent.subQuery && !( include.hasParentRequired && include.hasParentWhere ) ) && self.quoteIdentifier(tableLeft + "." + attrLeft) || ( subQuery && !include.subQuery && include.parent.subQuery && !( include.hasParentRequired && include.hasParentWhere ) ) && self.quoteIdentifier(tableLeft + "." + attrLeft) ||
...@@ -785,18 +785,25 @@ module.exports = (function() { ...@@ -785,18 +785,25 @@ module.exports = (function() {
) )
// Generate join SQL // Generate join SQL
joinQueryItem += joinType + self.quoteTable(table, as) + " ON " joinQueryItem += joinType + self.quoteTable(table, as) + " ON " + joinOn
joinQueryItem += where
if (include.where) { if (include.where) {
joinQueryItem += " AND "+self.getWhereConditions(include.where, self.sequelize.literal(self.quoteIdentifier(as)), include.daoFactory, whereOptions) joinQueryItem += " AND " + ( where = self.getWhereConditions(include.where, self.sequelize.literal(self.quoteIdentifier(as)), include.daoFactory, 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) {
if (!options.where) options.where = {} if (!options.where) options.where = {}
// 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("(SELECT "+self.quoteIdentifier(attrRight)+" FROM " + self.quoteTable(table, as) + " WHERE " + where + " LIMIT 1) IS NOT NULL") options.where["__"+as] = self.sequelize.asIs([ '(',
"SELECT " + self.quoteIdentifier(attrRight),
"FROM " + self.quoteTable(table, as),
"WHERE " + joinOn,
"AND " + where,
"LIMIT 1",
')', 'IS NOT NULL'].join(' '))
} }
} }
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!