Using table name qualified column names unless having SELECT list explicitly

Previously table name qualified `*` is used in that case. If it is not
qualified with a table name, an ambiguous column name error will occur
when using JOINs.
This commit is contained in:
Ryuta Kamizono 2017-12-18 01:40:55 +09:00
parent 475a0f887c
commit de354cc357
2 changed files with 9 additions and 5 deletions

@ -1040,8 +1040,8 @@ def convert_join_strings_to_ast(table, joins)
def build_select(arel)
if select_values.any?
arel.project(*arel_columns(select_values.uniq))
elsif @klass.ignored_columns.any?
arel.project(*arel_columns(@klass.column_names.map(&:to_sym)))
elsif klass.ignored_columns.any?
arel.project(*klass.column_names.map { |field| arel_attribute(field) })
else
arel.project(table[Arel.star])
end

@ -1498,10 +1498,14 @@ def test_default_values_are_deeply_dupped
test "column names are quoted when using #from clause and model has ignored columns" do
refute_empty Developer.ignored_columns
query = Developer.from("`developers`").to_sql
quoted_id = Developer.connection.quote_table_name("id")
query = Developer.from("developers").to_sql
quoted_id = "#{Developer.quoted_table_name}.#{Developer.quoted_primary_key}"
assert_match(/SELECT #{quoted_id}.* FROM `developers`/, query)
assert_match(/SELECT #{quoted_id}.* FROM developers/, query)
end
test "using table name qualified column names unless having SELECT list explicitly" do
assert_equal developers(:david), Developer.from("developers").joins(:shared_computers).take
end
test "protected environments by default is an array with production" do