Repository navigation
Closed
Labels
Description
What you are doing?
I'm performing a query on a model having two associated models. After joining on the associated models, I'd like to include a where clause with some conditional. I want a query like this one
SELECT *
FROM "A"
LEFT OUTER JOIN "B" ON aCondition
LEFT OUTER JOIN "C" ON anotherCondition
AND/OR conditions...
WHERE ("B"."id" IS NOT NULL OR "C"."someColumn" > 0)
Using Sequelize:
A.findAll({
where: {
$or: [
{'$B.someColumn$' : someCondition},
{'$C.someOtherColumn$' : someOtherCondition}
]
},
include: [{
model: B,
required: false
}, {
model: C,
required: false
}]
});
What do you expect to happen?
I'm expecting a query like the one above, but I get one like this, with the where clause in a subquery:
SELECT "Dataset".*,
"DatasetGroup"."id" AS "DatasetGroup.id",
"DatasetGroup.UserAccesses"."id" AS "DatasetGroup.UserAccesses.id",
"DatasetGroup.UserAccesses"."createdAt" AS "DatasetGroup.UserAccesses.createdAt",
"DatasetGroup.UserAccesses"."updatedAt" AS "DatasetGroup.UserAccesses.updatedAt",
"DatasetGroup.UserAccesses"."totalRequirements" AS "DatasetGroup.UserAccesses.totalRequirements",
"DatasetGroup.UserAccesses"."requirementsCompleted" AS "DatasetGroup.UserAccesses.requirementsCompleted",
"DatasetGroup.UserAccesses"."highestAction" AS "DatasetGroup.UserAccesses.highestAction",
"DatasetGroup.UserAccesses"."UserId" AS "DatasetGroup.UserAccesses.UserId",
"DatasetGroup.UserAccesses"."DatasetGroupId" AS "DatasetGroup.UserAccesses.DatasetGroupId",
"Stars"."id" AS "Stars.id",
"Stars"."createdAt" AS "Stars.createdAt",
"Stars"."updatedAt" AS "Stars.updatedAt",
"Stars"."UserId" AS "Stars.UserId",
"Stars"."DatasetId" AS "Stars.DatasetId"
FROM
(SELECT "Dataset"."id",
"Dataset"."isPublished",
"Dataset"."OrganizationId",
"Dataset"."DatasetGroupId",
"Dataset"."UserId",
"Dataset"."name",
"Dataset"."HeadTableId"
FROM "Datasets" AS "Dataset"
WHERE ("DatasetGroups.UserAccesses"."requirementsCompleted" > 0
OR "Stars"."id" IS NOT NULL) LIMIT 10
OFFSET 0) AS "Dataset"
LEFT OUTER JOIN "DatasetGroups" AS "DatasetGroup" ON "Dataset"."DatasetGroupId" = "DatasetGroup"."id"
LEFT OUTER JOIN "UserAccesses" AS "DatasetGroup.UserAccesses" ON "DatasetGroup"."id" = "DatasetGroup.UserAccesses"."DatasetGroupId"
AND "DatasetGroup.UserAccesses"."UserId" = 61
AND "DatasetGroup.UserAccesses"."requirementsCompleted" > 0
LEFT OUTER JOIN "Stars" AS "Stars" ON "Dataset"."id" = "Stars"."DatasetId"
AND "Stars"."UserId" = 61
Dialect: postgres
Database version: 9.5.3
Sequelize version: 4.1.0
Reacted by Eduardo Vicente, Gabriel Lebec, Husnul Anwari, Bosn, v1shva and Muhammad Zaid
Activity
haijianyang commentedon Jul 5, 2017
meet the same question too.
glebec commentedon Aug 22, 2017
I think I may be experiencing something similar. In Sequelize version 3, my app had this code:
Which generated this SQL:
Now in Sequelize v4, I switched it to this code:
Which I would have thought is equivalent, but it generates this SQL:
Unfortunately, the above SQL does not filter out all rows based on artist ID, as desired, but instead includes multiple other albums by different artists.
Why has the v4 behavior changed? I can fix the above with an additional
where : {artistId : this.id},but I don't see why I should have to.stale commentedon Sep 22, 2017
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂
ramakrishna-dadi commentedon Mar 5, 2018
@friend0 , I want query like what you get.
select uid,col1,col2,col3 from( select A.id as uid,col1,col2,col3 from A inner join B in A.id = B.mid where somecondition ) as 't' inner join users as u on t.uid = u.idI used your code
`models.A.findAll({
where: {
},
But i did n't get like that.
tizzyapunkt commentedon Jul 26, 2018
Currently facing the same issue as @friend0. Any news on that?
friend0 commentedon Jul 26, 2018
@tizzyapunkt @haijianyang My memory is hazy on this, but I believe you can do
tizzyapunkt commentedon Jul 26, 2018
@friend0 you saved my day!
The subquery flag is not documented. The only reference is #1756
danielkhaleb commentedon May 21, 2019
@friend0 you saved my day too! Thank you, man!
styts commentedon Feb 5, 2020
@friend0 Thank you from 2020!
Leeiio commentedon Apr 6, 2020
@friend0 Thank you from 2020 too!
abelptvts commentedon May 20, 2020
@friend0 thanks, mate!
raviCodelux commentedon Nov 26, 2021
Thanks from 2021 end, almost 2022!!
abdulrab09 commentedon Jul 26, 2022
@friend0 subQuery: false breaks the limit and offset, in your expected query, did you find any solution to make limit and offset work with subQuery: false?
aritalopes commentedon Aug 3, 2022
Experiencing the same as abdulrab09
malord commentedon Sep 1, 2022
raw: truefixes the limit and offset issue withsubQuery: false(well, in my case anyway)