aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-11-05 10:29:18 -0700
committerJon Leighton <j@jonathanleighton.com>2011-11-05 10:29:18 -0700
commitf575e21025c8e15b5f26b0dbb1eea82797bab632 (patch)
tree96e960b6d3ac6c6cab52485520ebe65340af041d /activerecord
parentf140445b1d2c4bf30bf5b0f28b9461773757b933 (diff)
parentfb0bf3c984fb088b5e436b25e6e5ae042e99c523 (diff)
downloadrails-f575e21025c8e15b5f26b0dbb1eea82797bab632.tar.gz
rails-f575e21025c8e15b5f26b0dbb1eea82797bab632.tar.bz2
rails-f575e21025c8e15b5f26b0dbb1eea82797bab632.zip
Merge pull request #3525 from kennyj/fix_3440-1
Fixed an issue id false option is ignored on mysql/mysql2 (fix #3440)
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md4
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb13
-rw-r--r--activerecord/test/cases/schema_dumper_test.rb5
3 files changed, 20 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 2524082062..6b1cf4c841 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -85,6 +85,10 @@
*Jon Leighton*
+* MySQL: use the information_schema than the describe command when we look for a primary key. *GH #3440*
+
+ *Kenny J*
+
## Rails 3.1.1 (October 7, 2011) ##
* Add deprecation for the preload_associations method. Fixes #3022.
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 35c2118190..306b185c5e 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
@@ -502,8 +502,17 @@ module ActiveRecord
# Returns a table's primary key and belonging sequence.
def pk_and_sequence_for(table)
- execute_and_free("DESCRIBE #{quote_table_name(table)}", 'SCHEMA') do |result|
- keys = each_hash(result).select { |row| row[:Key] == 'PRI' }.map { |row| row[:Field] }
+ sql = <<-SQL
+ SELECT t.constraint_type, k.column_name
+ FROM information_schema.table_constraints t
+ JOIN information_schema.key_column_usage k
+ USING (constraint_name, table_schema, table_name)
+ WHERE t.table_schema = DATABASE()
+ AND t.table_name = '#{table}'
+ SQL
+
+ execute_and_free(sql, 'SCHEMA') do |result|
+ keys = each_hash(result).select { |row| row[:constraint_type] == 'PRIMARY KEY' }.map { |row| row[:column_name] }
keys.length == 1 ? [keys.first, nil] : nil
end
end
diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb
index 71ff727b7f..5c3a78688e 100644
--- a/activerecord/test/cases/schema_dumper_test.rb
+++ b/activerecord/test/cases/schema_dumper_test.rb
@@ -238,4 +238,9 @@ class SchemaDumperTest < ActiveRecord::TestCase
assert_match %r(:id => false), match[1], "no table id not preserved"
assert_match %r{t.string[[:space:]]+"id",[[:space:]]+:null => false$}, match[2], "non-primary key id column not preserved"
end
+
+ def test_schema_dump_keeps_id_false_when_id_is_false_and_unique_not_null_column_added
+ output = standard_dump
+ assert_match %r{create_table "subscribers", :id => false}, output
+ end
end