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

You need to sign in or sign up before continuing.
Commit 73999181 by Mick Hansen

Merge branch 'tremby-issue-1953-findandcountall-test'

2 parents 1a81cee9 a573d39a
......@@ -1062,7 +1062,6 @@ module.exports = (function() {
// Filter statement
// Used by both join and subquery where
if (subQuery && !include.subQuery && include.parent.subQuery && (include.hasParentRequired || include.hasParentWhere || include.parent.hasIncludeRequired || include.parent.hasIncludeWhere)) {
joinOn = self.quoteIdentifier(tableLeft + '.' + attrLeft);
} else {
......@@ -1082,26 +1081,27 @@ module.exports = (function() {
if (include.where) {
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 (subQuery && association.isMultiAssociation && include.required) {
if (!options.where) options.where = {};
// Creating the as-is where for the subQuery, checks that the required association exists
var $query = self.selectQuery(include.model.getTableName(), {
tableAs: as,
attributes: [attrRight],
where: self.sequelize.asIs([joinOn]),
limit: 1
}, include.model);
options.where['__' + as] = self.sequelize.asIs([
'(',
$query.replace(/\;$/, ""),
')',
'IS NOT NULL'
].join(' '));
}
// 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 (subQuery && association.isMultiAssociation && include.required) {
if (!options.where) options.where = {};
// Creating the as-is where for the subQuery, checks that the required association exists
var $query = self.selectQuery(include.model.getTableName(), {
tableAs: as,
attributes: [attrRight],
where: self.sequelize.asIs([joinOn]),
limit: 1
}, include.model);
options.where['__' + as] = self.sequelize.asIs([
'(',
$query.replace(/\;$/, ""),
')',
'IS NOT NULL'
].join(' '));
}
// Generate join SQL
joinQueryItem += joinType + self.quoteTable(table, as) + ' ON ' + joinOn;
}
......
......@@ -176,5 +176,41 @@ describe(Support.getTestDialectTeaser("Include"), function () {
expect(result.count).to.equal(1);
});
});
})
})
it("should return the correct count and rows when using a required belongsTo and a limit", function() {
var s = this.sequelize
, Foo = s.define('Foo', {})
, Bar = s.define('Bar', {});
Foo.hasMany(Bar);
Bar.belongsTo(Foo);
return s.sync({ force: true }).then(function() {
// Make five instances of Foo
return Foo.bulkCreate([{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}]);
}).then(function() {
// Make four instances of Bar, related to the last four instances of Foo
return Bar.bulkCreate([{'FooId': 2}, {'FooId': 3}, {'FooId': 4}, {'FooId': 5}]);
}).then(function() {
// Query for the first two instances of Foo which have related Bars
return Foo.findAndCountAll({
include: [{ model: Bar, required: true }],
limit: 2
}).tap(function () {
return Foo.findAll({
include: [{ model: Bar, required: true }],
limit: 2
}).then(function (items) {
expect(items.length).to.equal(2);
});
});
}).then(function(result) {
expect(result.count).to.equal(4);
// The first two of those should be returned due to the limit (Foo
// instances 2 and 3)
expect(result.rows.length).to.equal(2);
});
});
});
});
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!