Repository navigation
Closed
Labels
pending-approvalBug reports that have not been verified yet, or feature requests that have not been accepted yet
Description
Issue Creation Checklist
- I understand that my issue will be automatically closed if I don't fill in the requested information
- I have read the contribution guidelines
Bug Description
limit does not work correctly when left joining to a subquery that contains inner joins. An additional where clause is added to the query that returns the incorrect results
Reproducible Example
Here is the link to the SSCCE for this issue:
What do you expect to happen?
In the below query, I expect a subquery join to be created such that child records are optional but only if they contain a related granchild.
Parents.findAndCountAll({
include: {
model: Children,
required: false,
include: [{
model: GrandChildren,
required: true,
}]
},
limit: 1
})What is actually happening?
An additional where clause is injected which causes the left join to be irrelevant.
select
`Parents`.*,
`Children`.`id` as `Children.id`,
`Children`.`parent_id` as `Children.parentId`,
`Children`.`parent_id` as `Children.ParentId`,
`Children->GrandChildren`.`id` as `Children.GrandChildren.id`,
`Children->GrandChildren`.`parent_id` as `Children.GrandChildren.parentId`,
`Children->GrandChildren`.`child_id` as `Children.GrandChildren.ChildId`
from
(
select
`Parents`.`id`,
`Parents`.`name`
from
`parents` as `Parents`
where
/** Because children is a left join, this where clause
should not be here **/
(
select
`Children`.`parent_id`
from
`children` as `Children`
inner join `grand_children` as `GrandChildren` on
`Children`.`id` = `GrandChildren`.`child_id`
where
(`Children`.`parent_id` = `Parents`.`id`)
limit 1 ) is not null
limit 1) as `Parents`
/** The join is correct **/
left outer join (
`children` as `Children`
inner join `grand_children` as `Children->GrandChildren`
on `Children`.`id` = `Children->GrandChildren`.`child_id`
)
on `Parents`.`id` = `Children`.`parent_id`;Environment
- Sequelize version:
- Node.js version:
- If TypeScript related: TypeScript version:
- Database & Version:
- Connector library & Version:
Would you be willing to resolve this issue by submitting a Pull Request?
- Yes, I have the time and I know how to start.
- Yes, I have the time but I will need guidance.
- No, I don't have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
- No, I don't have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in resolving my issue.
Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.
Reacted by Vincent Smith, Rick Waterman and Javier Sánchez Ochando
Metadata
Metadata
Assignees
Labels
pending-approvalBug reports that have not been verified yet, or feature requests that have not been accepted yet
Activity
SippieCup commentedon Oct 19, 2025
Add a test for this in my PR, new join generation fixes the issue.
SippieCup commentedon Nov 15, 2025
@WikiRik this can be closed