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

Commit f5ac4be0 by Mick Hansen

fix(find/include): fixes #2802 for mysql/postgres/mssql

1 parent ca51dd39
...@@ -1076,7 +1076,8 @@ module.exports = (function() { ...@@ -1076,7 +1076,8 @@ module.exports = (function() {
, attrRight = association.associationType !== 'BelongsTo' ? , attrRight = association.associationType !== 'BelongsTo' ?
association.identifierField || association.identifier : association.identifierField || association.identifier :
right.rawAttributes[primaryKeysRight[0]].field || primaryKeysRight[0] right.rawAttributes[primaryKeysRight[0]].field || primaryKeysRight[0]
, joinOn; , joinOn
, subQueryJoinOn;
// Filter statement // Filter statement
// Used by both join and subquery where // Used by both join and subquery where
...@@ -1094,11 +1095,14 @@ module.exports = (function() { ...@@ -1094,11 +1095,14 @@ module.exports = (function() {
} }
joinOn = self.quoteTable(tableLeft) + '.' + self.quoteIdentifier(attrLeft); joinOn = self.quoteTable(tableLeft) + '.' + self.quoteIdentifier(attrLeft);
} }
subQueryJoinOn = self.quoteTable(tableLeft) + '.' + self.quoteIdentifier(attrLeft);
joinOn += ' = ' + self.quoteTable(tableRight) + '.' + self.quoteIdentifier(attrRight); joinOn += ' = ' + self.quoteTable(tableRight) + '.' + self.quoteIdentifier(attrRight);
subQueryJoinOn += ' = ' + 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);
subQueryJoinOn += ' AND ' + self.getWhereConditions(include.where, self.sequelize.literal(self.quoteIdentifier(as)), include.model, whereOptions);
} }
// If its a multi association and the main query is a subquery (because of limit) we need to filter based on this association in a subquery // If its a multi association and the main query is a subquery (because of limit) we need to filter based on this association in a subquery
...@@ -1108,7 +1112,7 @@ module.exports = (function() { ...@@ -1108,7 +1112,7 @@ module.exports = (function() {
var $query = self.selectQuery(include.model.getTableName(), { var $query = self.selectQuery(include.model.getTableName(), {
tableAs: as, tableAs: as,
attributes: [attrRight], attributes: [attrRight],
where: self.sequelize.asIs([joinOn]), where: self.sequelize.asIs(subQueryJoinOn ? [subQueryJoinOn] : [joinOn]),
limit: 1 limit: 1
}, include.model); }, include.model);
......
...@@ -50,7 +50,7 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -50,7 +50,7 @@ describe(Support.getTestDialectTeaser('Include'), function() {
}); });
}); });
it('should work with a nested set of 1:M relations with a where on the last include', function () { it('should work with a 1:M to M:1 relation with a where on the last include', function () {
var Model = this.sequelize.define("Model", {}); var Model = this.sequelize.define("Model", {});
var Model2 = this.sequelize.define("Model2", {}); var Model2 = this.sequelize.define("Model2", {});
var Model4 = this.sequelize.define("Model4", {something: { type: DataTypes.INTEGER }}); var Model4 = this.sequelize.define("Model4", {something: { type: DataTypes.INTEGER }});
...@@ -61,8 +61,8 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -61,8 +61,8 @@ describe(Support.getTestDialectTeaser('Include'), function() {
Model2.hasMany(Model4); Model2.hasMany(Model4);
Model4.belongsTo(Model2); Model4.belongsTo(Model2);
return this.sequelize.sync({force: true}).then(function() { return this.sequelize.sync({force: true}).bind(this).then(function() {
Model.find({ return Model.find({
include: [ include: [
{model: Model2, include: [ {model: Model2, include: [
{model: Model4, where: {something: 2}} {model: Model4, where: {something: 2}}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!