Fixed an issue id false option is ignored on mysql/mysql2 (fix #3440)

This commit is contained in:
kennyj
2011-10-31 22:53:47 +09:00
parent cb06727b6c
commit fb0bf3c984
3 changed files with 20 additions and 2 deletions

View File

@@ -67,6 +67,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.

View File

@@ -496,8 +496,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

View File

@@ -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