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

bcahlit · May 01, 2019 · Last by bcahlit replied at May 01, 2019 · 883 hits

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

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 }
Reply to xinyifly

非常感谢,

bcahlit closed this topic. 01 May 21:28
bcahlit closed this topic. 01 May 21:29
bcahlit closed this topic. 01 May 21:29
bcahlit closed this topic. 01 May 21:29
bcahlit closed this topic. 01 May 21:29
bcahlit closed this topic. 01 May 21:29
You need to Sign in before reply, if you don't have an account, please Sign up first.