Rails 6.1 迁移。 add_reference 的参数数量错误(给定 3 个,预期 2 个)

问题描述 投票:0回答:1

这是最奇怪的事情...

我在迁移中这样做了

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

它起作用了。第三个论点从何而来??

ruby-on-rails migration ruby-on-rails-6.1 argument-error
1个回答
0
投票

错误是正确的...

正确使用方法

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 })

参考 https://api.rubyonrails.org/v7.0.7.2/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-add_reference

© www.soinside.com 2019 - 2024. All rights reserved.