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
ActiveRecord fails to include models given on a particular order after a join #30603
Comments
Can you provide an executable test script using this template? |
Couldn't recreate the issue. Here is my executable test script, @kamipo , https://gist.github.com/Bartuz/175845efab2ab3f3bc6b808cced7f571 @clemthenem , are you sure it isn't something about your applicationRecord? |
@Bartuz , the reason your script did not replicate the issue is because your code creates a query but doesn't execute it. If you append a ruby command like " |
Using ruby 2.4.2, I was able to replicate the behavior that @clemthenem described by using @Bartuz 's script with " > ProfileTranslation.joins(:profile).includes(:user, :profile).map(&:class)
SELECT "profile_translations"."id" AS t0_r0, "profile_translations"."profile_id" AS t0_r1,
"users"."id" AS t1_r0, "profiles_profile_translations"."id" AS t2_r0,
"profiles_profile_translations"."user_id" AS t2_r1
FROM "profile_translations"
INNER JOIN "profiles" ON "profiles"."id" = "profile_translations"."profile_id"
LEFT OUTER JOIN "profiles" "profiles_profile_translations_join"
ON "profiles_profile_translations_join"."id" = "profile_translations"."profile_id"
LEFT OUTER JOIN "users"
ON "users"."id" = "profiles_profile_translations_join"."user_id" And the contrasting example did produce SQL without the phantom table: > ProfileTranslation.joins(:profile).includes(:profile, :user).map(&:class)
SELECT "profile_translations"."id" AS t0_r0, "profile_translations"."profile_id" AS t0_r1,
"profiles"."id" AS t1_r0, "profiles"."user_id" AS t1_r1, "users"."id" AS t2_r0
FROM "profile_translations"
INNER JOIN "profiles" ON "profiles"."id" = "profile_translations"."profile_id"
LEFT OUTER JOIN "profiles" "profiles_profile_translations_join"
ON "profiles_profile_translations_join"."id" = "profile_translations"."profile_id"
LEFT OUTER JOIN "users"
ON "users"."id" = "profiles_profile_translations_join"."user_id" I don't know enough to evaluate @clemthenem 's symptoms, nor whether the example is well-formed. |
Hi guys, |
This issue has been automatically marked as stale because it has not been commented on for at least three months. |
…ucted Currently, column aliases which is used for eager loading are calculated before constructing all table aliases in FROM clause. `JoinDependency#join_constraints` constructs table aliases for `joins` first, and then always re-constructs table aliases for eager loading. If both `joins` and eager loading are given a same table association, the re-construction would cause the discrepancy between column aliases and table aliases. To avoid the discrepancy, the column aliases should be calculated after all table aliases are constructed. Fixes #30603.
Was this fix released in rails 4 line or only 5.1.x, i am sorry not sure how to check that. |
This is fixed in master, and backported to 5-2-stable in 50036e6. |
Steps to reproduce
Setup
I have three models
Query
This query (which looks silly but seems valid to me) is to simplify a much bigger example.
Expected behavior
I would expect to get an ActiveRecord::Relation of ProfileTranslations along with their user and profile.
Actual behavior
The interesting part is that if I change the order or the included elements:
Then I get my expected result.
System configuration
Rails version:
gem 'rails', '~> 5.1.4'
Ruby version:
ruby '2.4.1'
The text was updated successfully, but these errors were encountered: