Skip to content
Browse files

Merge pull request #3833 from kennyj/fix_3678-3

Use show create table (fix 3678: third time).
  • Loading branch information...
2 parents 75981a9 + f3470b0 commit 2e902d52ebc70a121ef49cc042672b4facab16fa @jonleighton jonleighton committed Dec 3, 2011
Showing with 8 additions and 3 deletions.
  1. +8 −3 activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
View
11 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 2e902d5

Please sign in to comment.
Something went wrong with that request. Please try again.