From 2e3e2fc39cd93d875776069d2d8b8a51bb5b1e4e Mon Sep 17 00:00:00 2001 From: Jacob Dunphy Date: Thu, 15 Jan 2009 13:30:44 -0800 Subject: [PATCH] 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. --- .../connection_adapters/sqlite_adapter.rb | 2 +- activerecord/test/cases/copy_table_test_sqlite.rb | 11 +++++++++++ activerecord/test/schema/schema.rb | 7 ++++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb index 9387cf8..5390f49 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -306,7 +306,7 @@ module ActiveRecord 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| diff --git a/activerecord/test/cases/copy_table_test_sqlite.rb b/activerecord/test/cases/copy_table_test_sqlite.rb index f0cfb67..645963d 100644 --- a/activerecord/test/cases/copy_table_test_sqlite.rb +++ b/activerecord/test/cases/copy_table_test_sqlite.rb @@ -45,6 +45,17 @@ class CopyTableTest < ActiveRecord::TestCase 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 = {}) diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 8199cb8..35838a9 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -153,7 +153,12 @@ ActiveRecord::Schema.define do create_table :funny_jokes, :force => true do |t| 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 -- 1.6.0.1