[Backport][3-1-stable] Fix GH #4754. Remove double-quote characters around PK when using sql_mode=ANSI_QUOTES #4787

Merged
merged 1 commit into from Jan 31, 2012
@@ -576,7 +576,7 @@ def pk_and_sequence_for(table)
create_table = result.first[1]
if create_table.to_s =~ /PRIMARY KEY\s+\((.+)\)/
- keys = $1.split(",").map { |key| key.gsub(/`/, "") }
+ keys = $1.split(",").map { |key| key.gsub(/[`"]/, "") }
keys.length == 1 ? [keys.first, nil] : nil
else
nil
@@ -760,7 +760,7 @@ def pk_and_sequence_for(table) #:nodoc:
result.free
if create_table.to_s =~ /PRIMARY KEY\s+\((.+)\)/
- keys = $1.split(",").map { |key| key.gsub(/`/, "") }
+ keys = $1.split(",").map { |key| key.gsub(/[`"]/, "") }
keys.length == 1 ? [keys.first, nil] : nil
else
nil
@@ -171,4 +171,15 @@ def test_set_primary_key_with_no_connection
assert_equal 'foo', model.primary_key
end
+
+ if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
+ def test_primaery_key_method_with_ansi_quotes
+ con = ActiveRecord::Base.connection
+ con.execute("SET SESSION sql_mode='ANSI_QUOTES'")
+ assert_equal "id", con.primary_key("topics")
+ ensure
+ con.reconnect!
+ end
+ end
+
end