Permalink
Browse files

Use show create table.

  • Loading branch information...
1 parent 36cc1f7 commit f3470b08c9f72da56462f7857ef49501cb5f3201 @kennyj kennyj committed Nov 22, 2011
Showing with 8 additions and 3 deletions.
  1. +8 −3 activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
@@ -499,9 +499,14 @@ def show_variable(name)
# 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

0 comments on commit f3470b0

Please sign in to comment.