Skip to content

Commit e6493eb

Browse files
jdunphyNZKoz
authored andcommitted
Sqlite adapter's copy_table incorrectly attempts to recreate a primary key id (:id => true in the create_table) if an :id column is present, even if it isn't a primary_key.
This fix sets :id => false if there is an :id column, but it's not the primary_key. Signed-off-by: Michael Koziarski <michael@koziarski.com> [#1766 state:committed]
1 parent feed7b4 commit e6493eb

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ def move_table(from, to, options = {}, &block) #:nodoc:
306306
end
307307

308308
def copy_table(from, to, options = {}) #:nodoc:
309-
options = options.merge(:id => !columns(from).detect{|c| c.name == 'id'}.nil?)
309+
options = options.merge(:id => (!columns(from).detect{|c| c.name == 'id'}.nil? && 'id' == primary_key(from).to_s))
310310
create_table(to, options) do |definition|
311311
@definition = definition
312312
columns(from).each do |column|

activerecord/test/cases/copy_table_test_sqlite.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ def test_copy_table_without_primary_key
4646
test_copy_table('developers_projects', 'programmers_projects')
4747
end
4848

49+
def test_copy_table_with_id_col_that_is_not_primary_key
50+
test_copy_table('goofy_string_id', 'goofy_string_id2') do |from, to, options|
51+
original_id = @connection.columns('goofy_string_id').detect{|col| col.name == 'id' }
52+
copied_id = @connection.columns('goofy_string_id2').detect{|col| col.name == 'id' }
53+
assert_equal original_id.type, copied_id.type
54+
assert_equal original_id.sql_type, copied_id.sql_type
55+
assert_equal original_id.limit, copied_id.limit
56+
assert_equal original_id.primary, copied_id.primary
57+
end
58+
end
59+
4960
protected
5061
def copy_table(from, to, options = {})
5162
@connection.copy_table(from, to, {:temporary => true}.merge(options))

activerecord/test/schema/schema.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ def create_table(*args, &block)
154154
t.string :name
155155
end
156156

157+
create_table :goofy_string_id, :force => true, :id => false do |t|
158+
t.string :id, :null => false
159+
t.string :info
160+
end
161+
157162
create_table :items, :force => true do |t|
158163
t.column :name, :integer
159164
end

0 commit comments

Comments
 (0)