1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix duplicate aliases when using both INNER/LEFT JOINs

It should be shared the count of alias tracking in both INNER/LEFT JOINs
to avoid duplicate aliases.

Fixes #30504.
Closes #30410.
This commit is contained in:
Ryuta Kamizono 2017-10-23 22:20:58 +09:00
parent 9fedd01347
commit 40cadf52db
2 changed files with 9 additions and 3 deletions

View file

@ -924,7 +924,7 @@ module ActiveRecord
def build_arel(aliases) def build_arel(aliases)
arel = Arel::SelectManager.new(table) arel = Arel::SelectManager.new(table)
build_joins(arel, joins_values.flatten, aliases) unless joins_values.empty? aliases = build_joins(arel, joins_values.flatten, aliases) unless joins_values.empty?
build_left_outer_joins(arel, left_outer_joins_values.flatten, aliases) unless left_outer_joins_values.empty? build_left_outer_joins(arel, left_outer_joins_values.flatten, aliases) unless left_outer_joins_values.empty?
arel.where(where_clause.ast) unless where_clause.empty? arel.where(where_clause.ast) unless where_clause.empty?
@ -1011,9 +1011,10 @@ module ActiveRecord
string_joins = buckets[:string_join].map(&:strip).uniq string_joins = buckets[:string_join].map(&:strip).uniq
join_list = join_nodes + convert_join_strings_to_ast(manager, string_joins) join_list = join_nodes + convert_join_strings_to_ast(manager, string_joins)
alias_tracker = alias_tracker(join_list, aliases)
join_dependency = ActiveRecord::Associations::JoinDependency.new( join_dependency = ActiveRecord::Associations::JoinDependency.new(
klass, table, association_joins, alias_tracker(join_list, aliases) klass, table, association_joins, alias_tracker
) )
joins = join_dependency.join_constraints(stashed_association_joins, join_type) joins = join_dependency.join_constraints(stashed_association_joins, join_type)
@ -1021,7 +1022,7 @@ module ActiveRecord
manager.join_sources.concat(join_list) manager.join_sources.concat(join_list)
manager alias_tracker.aliases
end end
def convert_join_strings_to_ast(table, joins) def convert_join_strings_to_ast(table, joins)

View file

@ -27,6 +27,11 @@ class InnerJoinAssociationTest < ActiveRecord::TestCase
end end
end end
def test_construct_finder_sql_does_not_table_name_collide_on_duplicate_associations_with_left_outer_joins
sql = Person.joins(agents: :agents).left_outer_joins(agents: :agents).to_sql
assert_match(/agents_people_4/i, sql)
end
def test_construct_finder_sql_does_not_table_name_collide_with_string_joins def test_construct_finder_sql_does_not_table_name_collide_with_string_joins
sql = Person.joins(:agents).joins("JOIN people agents_people ON agents_people.primary_contact_id = people.id").to_sql sql = Person.joins(:agents).joins("JOIN people agents_people ON agents_people.primary_contact_id = people.id").to_sql
assert_match(/agents_people_2/i, sql) assert_match(/agents_people_2/i, sql)