Skip to content
This repository
Browse code

Merge pull request #3525 from kennyj/fix_3440-1

Fixed an issue id false option is ignored on mysql/mysql2 (fix #3440)
  • Loading branch information...
commit f575e21025c8e15b5f26b0dbb1eea82797bab632 2 parents f140445 + fb0bf3c
Jon Leighton authored November 05, 2011
4  activerecord/CHANGELOG.md
Source Rendered
@@ -85,6 +85,10 @@
85 85
 
86 86
     *Jon Leighton*
87 87
 
  88
+*   MySQL: use the information_schema than the describe command when we look for a primary key. *GH #3440*
  89
+
  90
+    *Kenny J*
  91
+
88 92
 ## Rails 3.1.1 (October 7, 2011) ##
89 93
 
90 94
 *   Add deprecation for the preload_associations method. Fixes #3022.
13  activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
@@ -502,8 +502,17 @@ def show_variable(name)
502 502
 
503 503
       # Returns a table's primary key and belonging sequence.
504 504
       def pk_and_sequence_for(table)
505  
-        execute_and_free("DESCRIBE #{quote_table_name(table)}", 'SCHEMA') do |result|
506  
-          keys = each_hash(result).select { |row| row[:Key] == 'PRI' }.map { |row| row[:Field] }
  505
+        sql = <<-SQL
  506
+          SELECT t.constraint_type, k.column_name
  507
+          FROM information_schema.table_constraints t
  508
+          JOIN information_schema.key_column_usage k
  509
+          USING (constraint_name, table_schema, table_name)
  510
+          WHERE t.table_schema = DATABASE()
  511
+            AND t.table_name   = '#{table}'
  512
+        SQL
  513
+
  514
+        execute_and_free(sql, 'SCHEMA') do |result|
  515
+          keys = each_hash(result).select { |row| row[:constraint_type] == 'PRIMARY KEY' }.map { |row| row[:column_name] }
507 516
           keys.length == 1 ? [keys.first, nil] : nil
508 517
         end
509 518
       end
5  activerecord/test/cases/schema_dumper_test.rb
@@ -238,4 +238,9 @@ def test_schema_dump_keeps_id_column_when_id_is_false_and_id_column_added
238 238
     assert_match %r(:id => false), match[1], "no table id not preserved"
239 239
     assert_match %r{t.string[[:space:]]+"id",[[:space:]]+:null => false$}, match[2], "non-primary key id column not preserved"
240 240
   end
  241
+
  242
+  def test_schema_dump_keeps_id_false_when_id_is_false_and_unique_not_null_column_added
  243
+    output = standard_dump
  244
+    assert_match %r{create_table "subscribers", :id => false}, output
  245
+  end
241 246
 end

0 notes on commit f575e21

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