Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #3533 from kennyj/fix_3440-2

Barckport to 3-1-stable: fixed an issue id false option is ignored on mysql/mysql2 (fix #3440)
  • Loading branch information...
commit 311a21b44f9a4c6fc331fe8a5cbae9bf0e71a4ba 2 parents 3cd33ea + fb73423
Jon Leighton jonleighton authored
3  activerecord/CHANGELOG.md
View
@@ -20,6 +20,9 @@
*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) ##
* Raise an exception if the primary key of a model in an association is needed
13 activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb
View
@@ -573,9 +573,18 @@ def show_variable(name)
# Returns a table's primary key and belonging sequence.
def pk_and_sequence_for(table)
keys = []
- result = execute("DESCRIBE #{quote_table_name(table)}", 'SCHEMA')
+ 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
+
+ result = execute(sql, 'SCHEMA')
result.each(:symbolize_keys => true, :as => :hash) do |row|
- keys << row[:Field] if row[:Key] == "PRI"
+ keys << row[:column_name] if row[:constraint_type] == "PRIMARY KEY"
end
keys.length == 1 ? [keys.first, nil] : nil
end
13 activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
View
@@ -756,9 +756,18 @@ def show_variable(name)
# Returns a table's primary key and belonging sequence.
def pk_and_sequence_for(table) #:nodoc:
keys = []
- result = execute("describe #{quote_table_name(table)}", 'SCHEMA')
+ 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
+
+ result = execute(sql, 'SCHEMA')
result.each_hash do |h|
- keys << h["Field"]if h["Key"] == "PRI"
+ keys << h["column_name"] if h["constraint_type"] == "PRIMARY KEY"
end
result.free
keys.length == 1 ? [keys.first, nil] : nil
5 activerecord/test/cases/schema_dumper_test.rb
View
@@ -239,4 +239,9 @@ def test_schema_dump_keeps_id_column_when_id_is_false_and_id_column_added
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
Please sign in to comment.
Something went wrong with that request. Please try again.