新手问题 数据迁移中需要使用两个外键指向同一个表应该怎么描述?

bcahlit · 2019年05月01日 · 最后由 bcahlit 回复于 2019年05月01日 · 883 次阅读

只有一个外键的话可以这样

class CreateOrderForms < ActiveRecord::Migration[5.2]
  def change
    create_table :purchase do |t|
      t.references :user, foreign_key: true, on_delete: :cascade
      t.timestamps
    end
  end
end

如果有多个那, 比如采购有一个操作员和一个审批员,两个都是 user 应该怎么表述?

查看了这个文档,但是还是没有搞明白, https://ruby-china.github.io/rails-guides/active_record_migrations.html#foreign-keys

问题等同于 在 column 中使用自定义的列名,便于搜索

t.references :operator, foreign_key: { to_table: :users }
t.references :reviewer, foreign_key: { to_table: :users }
xinyifly 回复

非常感谢,

bcahlit 关闭了讨论。 05月01日 21:28
bcahlit 关闭了讨论。 05月01日 21:29
bcahlit 关闭了讨论。 05月01日 21:29
bcahlit 关闭了讨论。 05月01日 21:29
bcahlit 关闭了讨论。 05月01日 21:29
bcahlit 关闭了讨论。 05月01日 21:29
需要 登录 后方可回复, 如果你还没有账号请 注册新账号