Open segiddins opened 8 months ago
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 7-2-stable
branch or on main
, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.
still an issue
I think I encountered the same issue.
With the following classes:
class Company
has_many :accounts
has_many :users, through: :accounts
end
class Account
has_many :users
belongs_to :company
end
class User
belongs_to :account
end
If I do:
company = Company.find(5)
company.users.joins(:accounts).any?
I get the following SQL query:
SELECT 1 AS one FROM "users"
INNER JOIN "accounts" ON "users"."account_id" = "accounts"."id"
INNER JOIN "accounts" "accounts_users" ON "accounts_users"."id" = "users"."account_id"
WHERE "accounts"."company_id" = 5 LIMIT 1
There are 2 joins for the same table, the second one (accounts_users
) being completely useless.
In more complexe cases, this second useless join can apparently slow down queries.
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 8-0-stable
branch or on main
, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.
still an issue
On Nov 3, 2024, at 2:32 AM, rails-bot[bot] @.***> wrote:
This issue has been automatically marked as stale because it has not been commented on for at least three months. The resources of the Rails team are limited, and so we are asking for your help. If you can still reproduce this error on the 8-0-stable branch or on main, please reply with all of the information you have about it in order to keep the issue open. Thank you for all your contributions.
— Reply to this email directly, view it on GitHub https://github.com/rails/rails/issues/51259#issuecomment-2453376448, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAO3H4UPA5UFTLPJ75IH6NLZ6X3SRAVCNFSM6AAAAABEIFY3WSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDINJTGM3TMNBUHA. You are receiving this because you authored the thread.
Steps to reproduce
Have the following set of related models, and use the
reverse_dependencies
relation. Observe (via the inline comments in the test case below) that there is an unnecessary/redundant joinExpected behavior
The additional join should not be added, since it's an inner join and the
"versions"."id" = "dependencies"."version_id"
restriction guarantees it does not add any additional filteringActual behavior
There is an unused join to
"versions" "versions_dependencies"
System configuration
Rails version: 7.1.3.2
Ruby version: 3.3.0