Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Set the primary key during #copy_table if necessary. Fixes [#2312]

  • Loading branch information...
commit 3188f81199bdfa0d1660c4e2664d822544f9e04e 1 parent c5bcb0d
@scally scally authored
View
6 activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
@@ -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|
View
8 activerecord/test/cases/adapters/sqlite3/copy_table_test.rb
@@ -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))
Please sign in to comment.
Something went wrong with that request. Please try again.