diff options
author | Yasuo Honda <yasuo.honda@gmail.com> | 2018-08-06 11:38:44 +0000 |
---|---|---|
committer | Yasuo Honda <yasuo.honda@gmail.com> | 2018-08-11 14:49:45 +0000 |
commit | 33ce73edf1d1b02f4f50fc5d7565590468167d76 (patch) | |
tree | cf883b45f96fc0262052b53148c919526a1d2417 /activerecord/lib | |
parent | 924f44371697c5fe1509a749e1187ad2c9257ab9 (diff) | |
download | rails-33ce73edf1d1b02f4f50fc5d7565590468167d76.tar.gz rails-33ce73edf1d1b02f4f50fc5d7565590468167d76.tar.bz2 rails-33ce73edf1d1b02f4f50fc5d7565590468167d76.zip |
SQLite3 adapter `alter_table` method restores foreign keys
Related to #33520
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb index bee74dc33d..38b1c31376 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb @@ -409,13 +409,14 @@ module ActiveRecord def alter_table(table_name, options = {}) altered_table_name = "a#{table_name}" + options_to_restore = { foreign_keys: foreign_keys(table_name) } caller = lambda { |definition| yield definition if block_given? } transaction do disable_referential_integrity do move_table(table_name, altered_table_name, options.merge(temporary: true)) - move_table(altered_table_name, table_name, &caller) + move_table(altered_table_name, table_name, options_to_restore, &caller) end end end @@ -446,6 +447,13 @@ module ActiveRecord primary_key: column_name == from_primary_key ) end + + if options[:foreign_keys] + options[:foreign_keys].each do |fk| + @definition.foreign_key(fk.to_table, fk.options) + end + end + yield @definition if block_given? end copy_table_indexes(from, to, options[:rename] || {}) |