Repository navigation
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
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:
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
- Sequelize version: [email protected]
- Node.js version: v20.15.1
- If TypeScript related: TypeScript version: [email protected]
- Database & Version: PostgreSQL 14.13
- Connector library & Version: [email protected]
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