这是最奇怪的事情...
我在迁移中这样做了
class AddImmuneVaccineToImmuneRecord < ActiveRecord::Migration[6.1]
def change
add_reference :immunisation_records, :immune_vaccine
end
end
并得到 ArgumentError:参数数量错误(给定 3 个,预期 2 个)
但是当我这么做的时候
class AddImmuneVaccineToImmuneRecord < ActiveRecord::Migration[6.1]
def change
add_column :immunisation_records, :immune_vaccine_id, :bigint
add_index :immunisation_records, :immune_vaccine_id
end
end
它起作用了。第三个论点从何而来??
错误是正确的...
正确使用方法
add_reference
-
Create a user_id bigint column without an index
add_reference(:products, :user, index: false)
Create a user_id string column
add_reference(:products, :user, type: :string)
Create supplier_id, supplier_type columns
add_reference(:products, :supplier, polymorphic: true)
Create a supplier_id column with a unique index
add_reference(:products, :supplier, index: { unique: true })
Create a supplier_id column with a named index
add_reference(:products, :supplier, index: { name: "my_supplier_index" })
Create a supplier_id column and appropriate foreign key
add_reference(:products, :supplier, foreign_key: true)
Create a supplier_id column and a foreign key to the firms table
add_reference(:products, :supplier, foreign_key: { to_table: :firms })