-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Join in combination with preload does not works as intended (basic usage) #6715
Comments
Even with just :
It does not works |
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the |
This comment was marked as abuse.
This comment was marked as abuse.
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the |
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the |
When I was trying to debug why this happen I just discovered a misoptimization : This query :
Should execute 2 requests on the db, it does 3 :
Account and Pet are in the main query but Account is also in a separate query, this might be optimized. |
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the |
https://github.com/go-gorm/gorm/blob/master/callbacks/preload.go#L195 The queried content seems to be cleared by |
I'll try to fix it. |
Hi, I was investigating too and @a631807682 is right, the preload seems ovverwrite the join. |
|
@black-06 any way to know if there is a join that has in commons associations between joins and preload, if it's the case not creating unecessary queries and so not ovveriding the previous scan ? |
We should find a way to know what has already been loaded, right? From what I understand it seems that the statement is modified between each callback because the Query callback has the joins and the preloads, but the preload callback no longer has the joins. |
We (with @alexisvisco) dug into the issue and found that preload is done recursively by creating a query for each relation (https://github.com/go-gorm/gorm/blob/master/callbacks/preload.go#L192).
Do you have any other solutions? I can implement this if everyone is ok with this 🙂 |
Can we just observe whether there is a relationship between Join and Preload? For the above example, when Join and Preload have the same relationship Account, Preload's query for Account can be ignored. Joins("Account").Preload("Manager.Account") |
Yes, I'm trying to do that, but as @remicaumette says
So I'm looking into it here. |
this works for me |
GORM Playground Link
go-gorm/playground#666
Description
Given this basic test :
The test fails.
It must not fail, the query is correctly formatted :
As you can see Account__Pet is here, but it is not loaded.
If you remove the preload it's working. If you replace
Joins("Account.Pet").
with a preloda it's works.The problem occure for a combination of join and preload.
Use case : optimizing queries, not performing preload for a join query.
Thanks for your amazing works, I might open a pull request for a feature that has nothing to do with that.
The text was updated successfully, but these errors were encountered: