在rails中添加级联删除

时间:2015-12-16 22:17:51

标签: sql ruby-on-rails migration

我有一个schema.rb:

add_foreign_key "dog", "trees"

我想说:

add_foreign_key "dog", "trees", on_delete: :cascade

我正在考虑这样的迁移:

class ModifyTreeRefFromDogs < ActiveRecord::Migration
  def change
    change_column  ...
  end
end

1 个答案:

答案 0 :(得分:1)

删除它并再次添加它(它是一个索引,没有信息丢失)。

class ModifyTreeRefFromDogs < ActiveRecord::Migration
  def change
    remove_foreign_key "dog", "trees"
    add_foreign_key "dog", "trees", on_delete: :cascade
  end
end

注意:这似乎是rails中外键的一个很好的参考,并且包含您尝试过的on_delete代码:Foreign Keys in Rails 4.2