Repository navigation
fix(query-generator): generate subQueryFilter even for nested joins - #9188
Conversation
|
@sushantdhiman FYI a new PR that supersedes #9165 |
ca34b53 to
8a1776c
Compare
sushantdhiman
left a comment
There was a problem hiding this comment.
I have a few code related changes but fix otherwise looks good to me.
Code is extracted away to its own method so it can be used in two different segments, but still I have not worked with this code segment and would like reviews from other maintainers
| : this.escape(attr); | ||
| } | ||
| if (options.include && attr.indexOf('.') === -1 && addTable) { | ||
| if (options.include && !_.isEmpty(options.include) && attr.indexOf('.') === -1 && addTable) { |
There was a problem hiding this comment.
just use !_.isEmpty(options.include) instead of options.include && !_.isEmpty(options.include)
| * table to the include table plus everything that's in required transitive closure of the | ||
| * given include. | ||
| */ | ||
| generateSubQueryFilter(include, includeAs, topLevelInfo) { |
There was a problem hiding this comment.
rename to _generateSubQueryFilter
| * For a given include hierarchy creates a copy of it where only the required includes | ||
| * are preserved. | ||
| */ | ||
| getRequiredClosure(include) { |
There was a problem hiding this comment.
prefix this one with _ as well
…d joins (sequelize#9157) fixes sequelize#9157 This code introduces generateSubQueryFilter() which builds upon code previously found in generateThroughJoin() and generateInclude() to guarantee that proper subQueryFilter even in scenarios where multiple levels of duplicating joins are nested in a query that uses LIMIT / OFFSET.
|
@sushantdhiman thanks for feedback. I updated the PR. |
|
@sunshinewyin I think after this PR would get released we can write about it in both #7344 and #3007 and ask everyone for the feedback. If everything would work fine for a week - I'll be happy to verify it on the bountysource in favor of @jharting. @jamiesoncj what do you think about that? |
|
🎉 This PR is included in version 4.37.4 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
This appears to have resolved my issue, however I'm experiencing fairly large query times. In this case, Collection and Tag are n:m polymorphic relationships. Which creates the following query |
|
@DanFerro That query actually looks pretty reasonable to me - Given what input you've provided (there's a lot to take into account, so it's a complicated query). If you upgraded from v3 or an old v4 you might also be affected by this part: Can you do an You might get better results by doing separate find and count queries - Some of the joins could be trimmed of the count querying, making it faster. You might also write the tag part as raw SQL - That would probably prevent the need for a subquery: where: {
....
db.literal(`EXISTS (
SELECT 1
FROM item_tag
WHERE "post"."id" = "item_tag"."taggableId"
AND "item_tag"."taggable" = 'post'
AND item_tag."tagId" IN (SELECT id FROM tag WHERE ...)
)`)
} |
|
REGRESSION: This PR has great job for its purpose but breaks findOne filter by association result as described in #9228 |
… clause (sequelize#9228) fixing regression introduced in sequelize#9188
|
@jamiesoncj it has been only 3 days since the fix for regression #9273 has been merged, and it was Friday. I think we should wait for a few working days to be sure that everything is finally fine |
|
👍 @Alexsey thanks - sounds good to me. Happy to let you decide once you're happy. |
|
@sunshinewyin It was an upstream to regression fix so I thought that they are related. Now I see that they aren't, at least not directly. So I agree with @jamiesoncj that we should allow the reward rightaway. @jharting I'm sorry I didn't rise this question in a week after #9247 was merged. I have never claimed a bounty but I think you should go throught the link that @jamiesoncj has provided and press the "Claim Bounty" button. Thank you for this fix - you have killed a giant here :) |
|
Glad to hear the fix works and helped others. I submitted the bounty claim. If it gets accepted I intend to donate the full bounty to the Sequelize team. |
|
I still cannot get the correct result. Generated query is: |
|
@Wrufesh this PR fixes subqueries. Your query has |
|
Sorry to say but still after I remove I have updated code and sql query generated. |
|
@Wrufesh please create a new issue with description of your tables and what the expected and wrong output looks like |
|
Hello, is the fix available in latest npm version? I pulled npm 4.37.6 and seeing that limit is ignored. |
Vovka19
left a comment
There was a problem hiding this comment.
I’m not sure what I need to do. I made a lot of moves. Now I had too many folders and other copies. I cannot collect it together to make database and something visual.
|
This issue is still there, I am getting an error, Unknown column when I am trying to do this. I am getting the following error
I was able to fix the issue temporarily by using |
|
An ordering of results fixes the issue, but yeah, it reproduces easily: |
|
I am also having a similar problem, I have to to still use subQuery: false |
Closes #9157
Please make sure to review and check all of these items:
npm run testornpm run test-DIALECTpass with this change (including linting)?Description of change
This code introduces
generateSubQueryFilter()which builds upon code previously developed ingenerateThroughJoin()andgenerateInclude()to guarantee that proper subQueryFilter even in scenarios where multiple levels of duplicating joins are nested in a query that uses LIMIT / OFFSET.Here's an example:
Consider the following query:
Project --> User and User --> Hobby are many-to-many queries.
For limit and offset to work properly with duplicating joins it is necessary to push the required joins and where clauses down to subQueryFilter. Currently, sequelize produces the following output:
Upon closer inspection this is wrong as the subQueryFilter (most nested query) does not join into the required Hobbies table nor does it check for
name: 'archery'. As a result a User that is associated with the Project but does not have association witharcheryHobby entity may come out of the subQuery. Such user would then be filtered out in the top-most query, resulting in an empty result set.SQL generated after this patch:
Notice how here the subQueryFilter joins into Hobies table and checks for
name=archery.I tested this patch extensively both in our project and with Sequelize testsuite. However, please review
generateSubQueryFiltercarefully as may have missed some special cases.