Elixir 外键:国家/地区id必须与相应的关联名称不同
当我运行mix-ecto.migrate时,我遇到一个错误,Elixir 外键:国家/地区id必须与相应的关联名称不同,elixir,phoenix-framework,ecto,Elixir,Phoenix Framework,Ecto,当我运行mix-ecto.migrate时,我遇到一个错误,(ArgumentError)外键:country\u id必须与对应的关联名称lib/ecto/schema不同。例如:1499:ecto.schema。\uuu属于\uuuu/4 我的目标是在mobiles表中添加两个外键,即conutry_id和country_code。我错过了什么 这是我的手机模式: schema "mobiles" do field :number, :string belongs_to :person,
(ArgumentError)外键:country\u id必须与对应的关联名称lib/ecto/schema不同。例如:1499:ecto.schema。\uuu属于\uuuu/4
我的目标是在mobiles表中添加两个外键,即conutry_id和country_code。我错过了什么
这是我的手机模式:
schema "mobiles" do
field :number, :string
belongs_to :person, Person.Person
belongs_to :country_id, Person.Country, foreign_key: :country_id
belongs_to :country_code, Person.Country, foreign_key: :country_code
has_many :mobile_audits, Person.MobileAudit, on_delete: :delete_all
timestamps()
end
schema "countries" do
field :code, :string
field :name, :string
field :iso_codes, :string
has_many :mobiles, Person.Mobile, on_delete: :delete_all
timestamps()
end
这是我的国家模式:
schema "mobiles" do
field :number, :string
belongs_to :person, Person.Person
belongs_to :country_id, Person.Country, foreign_key: :country_id
belongs_to :country_code, Person.Country, foreign_key: :country_code
has_many :mobile_audits, Person.MobileAudit, on_delete: :delete_all
timestamps()
end
schema "countries" do
field :code, :string
field :name, :string
field :iso_codes, :string
has_many :mobiles, Person.Mobile, on_delete: :delete_all
timestamps()
end
这是我的迁移脚本:
def change do
alter table(:mobiles) do
add :country_id, references(:countries, type: :binary_id, column: :id)
add :country_code, references(:countries, type: :string, column: :code)
end
end
多谢各位
belongs_to :country_id, Person.Country, foreign_key: :country_id
此宏调用的形式为所属:name、QueryAbleModuleName、opts
。现在在您的例子中,:name
参数和外键
选项具有相同的值。通常,外键
应该是\u id
如果表中有company\u id
字段,则调用应如下所示:
belongs_to :company, MyModule.Company, foreign_key: :company_id
因此,如果您想将代码> StutoIdI/<代码>和<代码>国家代码> /代码>作为关联名称,请考虑使用外键“代码> StutoIdID ID/CODE >和<代码> StutoCudioID < /Cord>,并确保数据库模式反映了这一点。p>
或者在你的语境中思考一些更有意义的事情