Repository navigation
Description
What are you doing?
I have two entities in many-to-many relationship:
const User = this.sequelize.define('User', {
name: {
type: DataTypes.STRING,
primaryKey: true
}
}, {timestamps: false});
const Project = this.sequelize.define('Project', {
name: {
type: DataTypes.STRING,
primaryKey: true
}
}, {timestamps: false});
User.belongsToMany(Project, {through: 'user_project'});
Project.belongsToMany(User, {through: 'user_project'});This is the test data:
User Alice is associated with projects: alpha, charlie
User Bob is associated with projects: bravo
When I query the model like this:
Project.findAll({
include: [{
model: User,
where: {
name: 'Alice'
}
}],
order: ['name']I get the expected projects (alpha, charlie).
The problem occurs when I add pagination support. If I add limit and offset to the query like this:
Project.findAll({
include: [{
model: User,
where: {
name: 'Alice'
}
}],
order: ['name'],
limit: 1,
offset: 1What do you expect to happen?
I expect to get the second project associated with Alice i.e. charlie
What is actually happening?
I am getting an empty result list.
The query that Sequelize generates is:
SELECT `Project`.*, `Users`.`name` AS `Users.name`, `Users->user_project`.`createdAt` AS `Users.user_project.createdAt`, `Users->user_project`.`updatedAt` AS `Users.user_project.updatedAt`, `Users->user_project`.`UserName` AS `Users.user_project.UserName`, `Users->user_project`.`ProjectName` AS `Users.user_project.ProjectName` FROM (SELECT `Project`.`name` FROM `Projects` AS `Project` WHERE ( SELECT `user_project`.`UserName` FROM `user_project` AS `user_project` INNER JOIN `Users` AS `User` ON `user_project`.`UserName` = `User`.`name` WHERE (`Project`.`name` = `user_project`.`ProjectName`) LIMIT 1 ) IS NOT NULL ORDER BY `Project`.`name` LIMIT 1, 1) AS `Project` INNER JOIN `user_project` AS `Users->user_project` ON `Project`.`name` = `Users->user_project`.`ProjectName` INNER JOIN `Users` AS `Users` ON `Users`.`name` = `Users->user_project`.`UserName` AND `Users`.`name` = 'Alice' ORDER BY `Project`.`name`;
Notice how a subquery that joins the Users table is generated but this subquery does not contain the name = 'Alice' condition. Therefore, project bravo comes out of that subquery even though that project is not associated with user Alice which results in empty result due to the top-level INNER JOIN.
My opinion is that the condition (on clause) on Users table needs to be pushed into the nested query also.
A testcase can be found here: https://github.com/jharting/sequelize/blob/find-all-limit-include-where-testcase/test/integration/include/findAll.test.js#L2101
Dialect: mysql/sqlite
Database version: 10.1.31
Sequelize version: 4.35.2
Tested with latest release: yes
Activity
testcase for sequelize#9157
fix(query-generator): propagate where clauses of included models to t…
fix(query-generator): use where clause of included entity in subquery (…
fix(query-generator): use where clause of included entity in subquery (…
fix(query-generator): generate subQueryFilter even for nested require…
fix(query-generator): generate subQueryFilter even for nested require…
fix(query-generator): generate subQueryFilter even for nested require…
fix(query-generator): generate subQueryFilter even for nested require…
fix(query-generator): generate subQueryFilter even for nested require…