Skip to content

Error when attempting order based on existinance of many-to-many relationship with another model #17587

Open

Description

@acolchagoff

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

I have a stack overflow about this: https://stackoverflow.com/questions/79145151/unable-to-order-based-on-joined-table-when-using-sequelize-literal

TLDR:
I have a many to many relationship between two models. I want a complete list of the first model sorted by if it has a relationship with the second model or not. I am able to do this in raw SQL, but when I do it in sequelize it generates an invalid query.

my code:

const page = 1;
const rowsPerPage = 30;
const sortByTagId = 1134;

await User.findAll({
  include: [ { association: User.Tags } ],
  order: [
    fn('BOOL_OR', where(col('usertags.tagId'), sortByTagId)),
    'ASC',
  ],
  group: ['UserModel.id'],
  offset: rowsPerPage * (page - 1),
  limit: rowsPerPage,
});

Reproducible Example

Here is the link to the SSCCE for this issue:

sequelize/sequelize-sscce#285

this test is a good example: https://github.com/sequelize/sequelize-sscce/actions/runs/11630941289/job/32390958905?pr=285

What do you expect to happen?

I expect a query similar to the following to be generated:

SELECT
      "UserModel".*
FROM "Users" AS "UserModel" 
    INNER JOIN "userTags" on "usertags"."userId" = "UserModel"."id"
WHERE ("UserModel"."deletedAt" IS NULL) 
GROUP BY "UserModel"."id"
ORDER BY bool_or("usertags"."tagId" = 1134) DESC 
LIMIT 30 OFFSET 0;

What is actually happening?

Error: error: missing FROM-clause entry for table "userTags"
and a query similar to the following is generated:

SELECT 
  "UserModel".*, 
  "usertags"."id" AS "_0",
  "usertags"."uuid" AS "_1",
  "usertags"."createdAt" AS "_2",
  "usertags"."deletedAt" AS "_3",
  "usertags"."updatedAt" AS "_4",
  "usertags"."tagId" AS "_6",
  "usertags"."userId" AS "_8"
FROM (
  SELECT 
    "UserModel"."id",
    "UserModel"."uuid",
    "UserModel"."createdAt",
    "UserModel"."deletedAt",
    "UserModel"."updatedAt",
    "UserModel"."status",
    "UserModel"."locationId",
    "UserModel"."firstName",
    "UserModel"."lastName",
    "UserModel"."suffix",
    "UserModel"."email", 
    "UserModel"."phone"
  FROM "users" AS "UserModel"
  WHERE ("UserModel"."deletedAt" IS NULL) 
    AND ( 
      SELECT "userId" 
      FROM "usertags" AS "usertags" 
      WHERE (
        "usertags"."deletedAt" IS NULL 
        AND "usertags"."userId" = "UserModel"."id"
      ) 
      LIMIT 1 
    ) IS NOT NULL 
  GROUP BY "UserModel"."id" 
  ORDER BY bool_or("usertags"."tagId" = 1134) ASC LIMIT 30 OFFSET 0
) AS "UserModel" 
  INNER JOIN "usertags" AS "usertags" ON "UserModel"."id" = "usertags"."userId" AND "usertags"."deletedAt" IS NULL 
ORDER BY bool_or("usertags"."tagId" = 1134) ASC;

Environment

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Maybe, I have limited time and 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 Nov 1, 2024
  2. added theissue type on Mar 9, 2025
  3. 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