提交 8ad58af4 编写于 作者: R Rafael Mendonça França

Merge pull request #6186 from anydiem/fix_unconventional_pks

Set the primary key during #copy_table if necessary. Fixes [#2312]
......@@ -522,7 +522,11 @@ def move_table(from, to, options = {}, &block) #:nodoc:
end
def copy_table(from, to, options = {}) #:nodoc:
options = options.merge(:id => (!columns(from).detect{|c| c.name == 'id'}.nil? && 'id' == primary_key(from).to_s))
from_primary_key = primary_key(from)
options[:primary_key] = from_primary_key if from_primary_key != 'id'
unless options[:primary_key]
options[:id] = columns(from).detect{|c| c.name == 'id'}.present? && from_primary_key == 'id'
end
create_table(to, options) do |definition|
@definition = definition
columns(from).each do |column|
......
......@@ -57,6 +57,14 @@ def test_copy_table_with_id_col_that_is_not_primary_key
end
end
def test_copy_table_with_unconventional_primary_key
test_copy_table('owners', 'owners_unconventional') do |from, to, options|
original_pk = @connection.primary_key('owners')
copied_pk = @connection.primary_key('owners_unconventional')
assert_equal original_pk, copied_pk
end
end
protected
def copy_table(from, to, options = {})
@connection.copy_table(from, to, {:temporary => true}.merge(options))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册