首页 > 解决方案 > Rails 自加入迁移

问题描述

我正在尝试使用 Amoeba gem 通过复制我的模型来创建草稿。我想做的是创建一个自联接,这样:

mainRecord.draft给出草稿,并 draftRecord.master给出主记录。

代码应该足够简单:

class AddMasterToOrganizations < ActiveRecord::Migration[6.1]
  def change
    add_reference :organizations, :master, foreign_key: { to_table: :organizations }
  end
end

然而,使用 strong_migrations,它希望我同时添加索引,如下所示:

class AddMasterToOrganizations < ActiveRecord::Migration[6.1]
  disable_ddl_transaction!

  def change
    add_reference :organizations, :master, index: {algorithm: :concurrently}
  end
end

如果我这样做,我不确定要创建什么其他迁移才能使外键正常工作。

标签: ruby-on-railsmigrationself-join

解决方案


推荐阅读