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

Commit cf967349 by Mick Hansen

fix(include): fix issue with bad query generation on empty include.where, closes #3099

1 parent 007d20bd
# Next
- [BUG] Fixed issue with empty `include.where`
# 2.0.0 # 2.0.0
- [BUG] Fixed `field` support for `increment` and `decrement`. - [BUG] Fixed `field` support for `increment` and `decrement`.
- [FEATURE/BUG] Raw queries always return all results (including affected rows etc). This means you should change all promise listeners on `sequelize.query` to use `.spread` instead of `.then`, unless you are passing a query type. - [FEATURE/BUG] Raw queries always return all results (including affected rows etc). This means you should change all promise listeners on `sequelize.query` to use `.spread` instead of `.then`, unless you are passing a query type.
......
...@@ -905,7 +905,8 @@ module.exports = (function() { ...@@ -905,7 +905,8 @@ module.exports = (function() {
, through = include.through , through = include.through
, joinType = include.required ? ' INNER JOIN ' : ' LEFT OUTER JOIN ' , joinType = include.required ? ' INNER JOIN ' : ' LEFT OUTER JOIN '
, includeWhere = {} , includeWhere = {}
, whereOptions = Utils._.clone(options); , whereOptions = Utils._.clone(options)
, targetWhere;
whereOptions.keysEscaped = true; whereOptions.keysEscaped = true;
...@@ -981,7 +982,6 @@ module.exports = (function() { ...@@ -981,7 +982,6 @@ module.exports = (function() {
, sourceJoinOn , sourceJoinOn
, targetJoinOn , targetJoinOn
, targetWhere
, throughWhere; , throughWhere;
...@@ -1034,7 +1034,9 @@ module.exports = (function() { ...@@ -1034,7 +1034,9 @@ module.exports = (function() {
if (include.where || include.through.where) { if (include.where || include.through.where) {
if (include.where) { if (include.where) {
targetWhere = self.getWhereConditions(include.where, self.sequelize.literal(self.quoteIdentifier(as)), include.model, whereOptions); targetWhere = self.getWhereConditions(include.where, self.sequelize.literal(self.quoteIdentifier(as)), include.model, whereOptions);
joinQueryItem += ' AND ' + targetWhere; if (targetWhere) {
joinQueryItem += ' AND ' + targetWhere;
}
} }
if (subQuery && include.required) { if (subQuery && include.required) {
if (!options.where) options.where = {}; if (!options.where) options.where = {};
...@@ -1148,8 +1150,11 @@ module.exports = (function() { ...@@ -1148,8 +1150,11 @@ module.exports = (function() {
subQueryJoinOn += ' = ' + 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); targetWhere = 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 (targetWhere) {
joinOn += ' AND ' + targetWhere;
subQueryJoinOn += ' AND ' + targetWhere;
}
} }
// 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
......
...@@ -2132,6 +2132,25 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -2132,6 +2132,25 @@ describe(Support.getTestDialectTeaser('Include'), function() {
}); });
}); });
it('should work with an empty include.where', function () {
var User = this.sequelize.define('User', {})
, Company = this.sequelize.define('Company', {})
, Group = this.sequelize.define('Group', {});
User.belongsTo(Company);
User.belongsToMany(Group);
Group.belongsToMany(User);
return this.sequelize.sync({force: true}).then(function () {
return User.findAll({
include: [
{model: Group, where: {}},
{model: Company, where: {}}
]
});
});
});
it('should be able to order on the main table and a required belongsTo relation with custom tablenames and limit ', function () { it('should be able to order on the main table and a required belongsTo relation with custom tablenames and limit ', function () {
var User = this.sequelize.define('User', { var User = this.sequelize.define('User', {
lastName: DataTypes.STRING lastName: DataTypes.STRING
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!