New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Limit parameter breaks eager loading where clause #11082
Comments
Related issue: #10962 |
Found a 2 solutions. One seems completely undocumented and the other I can only find very vague documentation. The first solution I only found here: #7778 (comment) await Model1.findAll({
where: {
"$Model2s.id$": null
},
subQuery: false,
limit: 1,
include: [
{
model: Model2,
required: false
}
]
}) The second solution is here in: #1756 (comment) -> Model Reference Docs await Model1.findAll({
where: {
"$Model2s.id$": null
},
limit: 1,
include: [
{
model: Model2,
required: false,
duplicating: false
}
]
}) Both solutions require the user to know they are even available (a real problem with the first). They also require the user to know that under the hood, Can this be adjusted to not require either of these 2 things in this case (since the generated query doesn't even work) or can appropriate documentation under |
@Goblinlordx the above solutions are non-solutions. Here is why: Both of the proposed solutions turn the following (erroneous) query: SELECT "main".*, "joined"."prop" AS "joined.prop"
FROM (
SELECT "main"....
FROM "main" AS "main"
WHERE "joined"."prop" = "foo"
LIMIT 10
OFFSET 10
) AS "main"
LEFT OUTER JOIN "joined" AS "joined" ON "joined"."foreign_key" = "main"."other_key" into the following: SELECT "main".*, "joined"."prop" AS "joined.prop"
FROM "main" as "main"
SELECT "main"....
FROM "main" AS "main"
) AS "main"
LEFT OUTER JOIN "joined" AS "joined" ON "joined"."foreign_key" = "main"."other_key"
WHERE "joined"."prop" = "foo"
LIMIT 10
OFFSET 10 This executes, but it isn't guaranteed to get the first 10 unique "main" results. Why? Because if one of the "main" results has more than one matching "joined" result, it will appear more than once in the result set. E.g. you could have the following full result set:
It would be from this result set that the You'd miss You'd also only get five results in your set, not 10 as you requested (which you should, assuming the full result set includes more than 10 unique main items). |
I believe this is a duplicate of #10962. |
Understood~ I hope your PR gets merged and released soon. It is a little disheartening that your issue has no comments other than your own and others with the issue and it is a month old. |
@felamaslen @Goblinlordx Thx guys and I do hope so that @felamaslen PR gets merged~ |
Closing in favor of #10962 |
What are you doing?
To Reproduce
Steps to reproduce the behavior:
Model1.findAll({ where: "$Model2s.id$": null, limit: 1, include: { model: Model2, required: false})
UnhandledPromiseRejectionWarning: SequelizeDatabaseError: SQLITE_ERROR: no such column: Model2s.id
In the example code above you can see the results. A query without
limit
works properly. A query withlimit
causes the error. It appears that it is putting thelimit
clause in the incorrect place in the query (should be after the where clause at the end) and instead moves the part of the where which contains the condition selecting the nested property into the inner where clause erroneously.Run:
With these files and node version 11+ I receive the described error only when it hits the last
findAll
query whenlimit
is added.What do you expect to happen?
I expect the query to be executed successfully without an error.
What is actually happening?
LIMIT
clause and the part of the where clause selecting the property from the eagerly loaded model are put on the inner where clause selection causing an error.UnhandledPromiseRejectionWarning: SequelizeDatabaseError: SQLITE_ERROR: no such column: Model2s.id
Environment
Dialect:
Dialect library version: N/A
Database version: N/A (occurs on both sqlite3 and mysql)
Sequelize version: 5.8.9
Node Version: 11.10.1
OS: N/A
If TypeScript related: TypeScript version: N/A
Tested with latest release:
The text was updated successfully, but these errors were encountered: