Skip to content

Combining limit With Subquery Joins Produces Incorrect Results #17943

Closed

Description

@Vince-Smith

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:

https://github.com/sequelize/sequelize-sscce/actions/runs/16475634024

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.

Activity

  1. added
    pending-approval
    on Jul 23, 2025
  2. added theissue type on Jul 23, 2025
  3. SippieCup commented on Oct 19, 2025

    @SippieCup
    Contributor

    Add a test for this in my PR, new join generation fixes the issue.

  4. SippieCup commented on Nov 15, 2025

    @SippieCup
    Contributor

    @WikiRik this can be closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    pending-approval

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions