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

Quote primary key in the subselect generated by mysql2 adapter

Otherwise it will occur syntax error if primary key is a reserved word.
This commit is contained in:
Ryuta Kamizono 2017-12-19 20:59:09 +09:00
parent 9e7260da1b
commit 0863335453
3 changed files with 12 additions and 4 deletions

View file

@ -717,7 +717,8 @@ module ActiveRecord
# to work with MySQL 5.7.6 which sets optimizer_switch='derived_merge=on'
subselect.distinct unless select.limit || select.offset || select.orders.any?
Arel::SelectManager.new(subselect.as("__active_record_temp")).project(Arel.sql(key.name))
key_name = quote_column_name(key.name)
Arel::SelectManager.new(subselect.as("__active_record_temp")).project(Arel.sql(key_name))
end
def supports_rename_index?

View file

@ -39,7 +39,7 @@ class ReservedWordTest < ActiveRecord::TestCase
t.string :order
t.belongs_to :select
end
@connection.create_table :values, force: true do |t|
@connection.create_table :values, primary_key: :as, force: true do |t|
t.belongs_to :group
end
end
@ -88,6 +88,13 @@ class ReservedWordTest < ActiveRecord::TestCase
assert_equal x, Group.find(x.id)
end
def test_delete_all_with_subselect
create_test_fixtures :values
assert_equal 1, Values.order(:as).limit(1).offset(1).delete_all
assert_raise(ActiveRecord::RecordNotFound) { Values.find(2) }
assert Values.find(1)
end
def test_has_one_associations
create_test_fixtures :group, :values
v = Group.find(1).values

View file

@ -1,7 +1,7 @@
values1:
id: 1
as: 1
group_id: 2
values2:
id: 2
as: 2
group_id: 1