我正在尝试通过迁移添加外键。它可以按预期工作,但是会自动在末尾添加_id
(我要引用的列名不包括_id
)。我如何赋予它引用列名的权限?
这是迁移
class ChangeRefOnMemberPresentations < ActiveRecord::Migration[5.2]
def change
add_reference :member_presentations, 'employee_number', foreign_key: { to_table: :users }
end
end
这会导致在schema.rb中引用列名称和外键引用列都被称为employee_number_id
答案 0 :(得分:1)
以下内容通过手动定义所有内容而起作用,但看起来很杂乱。如果有更好的迁移答案,我会很乐意接受。
def change
# column was added in another migration, but including for completness
add_column :member_presentations, :employee_number, :bigint
add_index :member_presentations, :employee_number, name: "index_member_presentations_on_employee_number"
add_foreign_key :member_presentations, :users, column: "employee_number"
end