Skip to content

Commit

Permalink
Sqlite adapter's copy_table incorrectly attempts to recreate a primar…
Browse files Browse the repository at this point in the history
…y 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>
[rails#1766 state:committed]
  • Loading branch information
jdunphy authored and NZKoz committed Jan 29, 2009
1 parent feed7b4 commit e6493eb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
Expand Up @@ -306,7 +306,7 @@ 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?)
options = options.merge(:id => (!columns(from).detect{|c| c.name == 'id'}.nil? && 'id' == primary_key(from).to_s))
create_table(to, options) do |definition|
@definition = definition
columns(from).each do |column|
Expand Down
11 changes: 11 additions & 0 deletions activerecord/test/cases/copy_table_test_sqlite.rb
Expand Up @@ -46,6 +46,17 @@ def test_copy_table_without_primary_key
test_copy_table('developers_projects', 'programmers_projects')
end

def test_copy_table_with_id_col_that_is_not_primary_key
test_copy_table('goofy_string_id', 'goofy_string_id2') do |from, to, options|
original_id = @connection.columns('goofy_string_id').detect{|col| col.name == 'id' }
copied_id = @connection.columns('goofy_string_id2').detect{|col| col.name == 'id' }
assert_equal original_id.type, copied_id.type
assert_equal original_id.sql_type, copied_id.sql_type
assert_equal original_id.limit, copied_id.limit
assert_equal original_id.primary, copied_id.primary
end
end

protected
def copy_table(from, to, options = {})
@connection.copy_table(from, to, {:temporary => true}.merge(options))
Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/schema/schema.rb
Expand Up @@ -154,6 +154,11 @@ def create_table(*args, &block)
t.string :name
end

create_table :goofy_string_id, :force => true, :id => false do |t|
t.string :id, :null => false
t.string :info
end

create_table :items, :force => true do |t|
t.column :name, :integer
end
Expand Down

0 comments on commit e6493eb

Please sign in to comment.