Skip to content

The request is not correctly generated when adding a limit #17440

Open

Description

@Bloblabl

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

Reproducible Example

When forming a query to a model, a communication error: "missing FROM-clause entry for table "fk1_Parents_Sketches"", occurs if you set a record limit

Sketches.findAll({ order: queryOrder, limit: length, offset: start, where: { '$fk1_Parents_Sketches.id$': null }, include: [ { model: Sketches, as: 'fk1_Parents_Sketches', attributes: [], required: false } ] })
When you add "limit" to a query, sequalize breaks the query

What do you expect to happen?

I would like to see a query without a subquery, just to put a limit value at the end of the query

SELECT "Sketches"."id", "fk1_Parents_Sketches->childSketches_parentSketches"."parentId" AS "fk1_Parents_Sketches.childSketches_parentSketches.parentId", "fk1_Parents_Sketches->childSketches_parentSketches"."childId" AS "fk1_Parents_Sketches.childSketches_parentSketches.childId" FROM "Sketches" AS "Sketches" LEFT OUTER JOIN ( "childSketches_parentSketches" AS "fk1_Parents_Sketches->childSketches_parentSketches" INNER JOIN "Sketches" AS "fk1_Parents_Sketches" ON "fk1_Parents_Sketches"."id" = "fk1_Parents_Sketches->childSketches_parentSketches"."parentId" ) ON "Sketches"."id" = "fk1_Parents_Sketches->childSketches_parentSketches"."childId" WHERE "fk1_Parents_Sketches"."id" IS NULL LIMIT '10' OFFSET 0;

What is actually happening?

SELECT "Sketches".*, "fk1_Parents_Sketches->childSketches_parentSketches"."parentId" AS "fk1_Parents_Sketches.childSketches_parentSketches.parentId", "fk1_Parents_Sketches->childSketches_parentSketches"."childId" AS "fk1_Parents_Sketches.childSketches_parentSketches.childId" FROM ( SELECT "Sketches"."id" FROM "Sketches" AS "Sketches" WHERE "fk1_Parents_Sketches"."id" IS NULL LIMIT '10' OFFSET 0 ) AS "Sketches" LEFT OUTER JOIN ( "childSketches_parentSketches" AS "fk1_Parents_Sketches->childSketches_parentSketches" INNER JOIN "Sketches" AS "fk1_Parents_Sketches" ON "fk1_Parents_Sketches"."id" = "fk1_Parents_Sketches->childSketches_parentSketches"."parentId" ) ON "Sketches"."id" = "fk1_Parents_Sketches->childSketches_parentSketches"."childId"

Environment

  • Sequelize version: 6.37.3
  • Node.js version: 20.11.1
  • If TypeScript related: TypeScript version:
  • Database & Version: PostgreSQL 16.1
  • Connector library & Version: pg 8.11.3

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
    type: bug
    on Aug 6, 2024
  2. wadeharrell commented on Oct 24, 2024

    @wadeharrell

    following the example from https://sequelize.org/docs/v6/advanced-association-concepts/eager-loading/#complex-where-clauses-at-the-top-level but adding a limit:

    User.findAll({
      limit: 10,
      where: {
        '$Instruments.size$': { [Op.ne]: 'small' },
      },
      include: [
        {
          model: Tool,
          as: 'Instruments',
        },
      ],
    });
    

    Will generate broken SQL using 6.37.4

  3. WikiRik commented on Oct 25, 2024

    @WikiRik
    Member

    This might be related to #12971 I think?

  4. added theissue type on Mar 9, 2025
  5. removed
    type: bug
    on Mar 9, 2025
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