diff options
author | Yves Senn <yves.senn@gmail.com> | 2013-02-23 14:00:02 +0100 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2013-02-23 15:23:28 +0100 |
commit | 8f6fa345e5c865acefa063c3e2cd62553fa9f2eb (patch) | |
tree | 6eeef63a41866e2571ccf4f5157d2abadd5e4e50 /activerecord/lib/active_record/connection_adapters | |
parent | 0c1558d32fe7f3198ef598ce552ea89a04ab7793 (diff) | |
download | rails-8f6fa345e5c865acefa063c3e2cd62553fa9f2eb.tar.gz rails-8f6fa345e5c865acefa063c3e2cd62553fa9f2eb.tar.bz2 rails-8f6fa345e5c865acefa063c3e2cd62553fa9f2eb.zip |
Sqlite preserves primary keys when copying/altering tables.
Backport #2312.
Fixes #9367.
I also added a test-case to make sure that renaming or removing
a column preserves the primary key.
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb | 8 |
1 files changed, 6 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 ca84c95bdc..455560aa55 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -490,7 +490,11 @@ module ActiveRecord 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'}.nil? && 'id' == from_primary_key + end create_table(to, options) do |definition| @definition = definition columns(from).each do |column| @@ -504,7 +508,7 @@ module ActiveRecord :precision => column.precision, :scale => column.scale, :null => column.null) end - @definition.primary_key(primary_key(from)) if primary_key(from) + @definition.primary_key(from_primary_key) if from_primary_key yield @definition if block_given? end |