diff options
author | kennyj <kennyj@gmail.com> | 2011-11-22 18:31:58 +0900 |
---|---|---|
committer | kennyj <kennyj@gmail.com> | 2011-12-03 02:21:56 +0900 |
commit | f3470b08c9f72da56462f7857ef49501cb5f3201 (patch) | |
tree | fbf91bf247ec79c77f9ff82e2bb7d3efdbce73d7 | |
parent | 36cc1f78eb17765f51e88085c30c39f40f3cb611 (diff) | |
download | rails-f3470b08c9f72da56462f7857ef49501cb5f3201.tar.gz rails-f3470b08c9f72da56462f7857ef49501cb5f3201.tar.bz2 rails-f3470b08c9f72da56462f7857ef49501cb5f3201.zip |
Use show create table.
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index 7f5ddf77d6..4d2c80356d 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -499,9 +499,14 @@ module ActiveRecord # Returns a table's primary key and belonging sequence. def pk_and_sequence_for(table) - execute_and_free("SHOW INDEX FROM #{quote_table_name(table)} WHERE Key_name = 'PRIMARY'", 'SCHEMA') do |result| - keys = each_hash(result).map { |row| row[:Column_name] } - keys.length == 1 ? [keys.first, nil] : nil + execute_and_free("SHOW CREATE TABLE #{quote_table_name(table)}", 'SCHEMA') do |result| + create_table = each_hash(result).first[:"Create Table"] + if create_table.to_s =~ /PRIMARY KEY\s+\((.+)\)/ + keys = $1.split(",").map { |key| key.gsub(/`/, "") } + keys.length == 1 ? [keys.first, nil] : nil + else + nil + end end end |