Elixir EXTO中“多对多”关联的外键名
这里有一个问题要问很多人。我得到的错误是Elixir EXTO中“多对多”关联的外键名,elixir,ecto,Elixir,Ecto,这里有一个问题要问很多人。我得到的错误是 INSERT INTO "qbinders" ("title","typecode","inserted_at","updated_at","id") VALUES ($1,$2,$3,$4,$5) ["Math", "Jim1000", {{2017, 4, 1}, {15, 4, 47, 827009}}, {{2017, 4, 1}, {15, 4, 47, 843348}}, <<68, 1 49, 156, 219, 153, 21
INSERT INTO "qbinders" ("title","typecode","inserted_at","updated_at","id") VALUES ($1,$2,$3,$4,$5) ["Math", "Jim1000", {{2017, 4, 1}, {15, 4, 47, 827009}}, {{2017, 4, 1}, {15, 4, 47, 843348}}, <<68, 1
49, 156, 219, 153, 214, 65, 63, 179, 141, 252, 147, 221, 247, 41, 143>>]
[debug] QUERY OK db=0.9ms
commit []
** (Postgrex.Error) ERROR 42703 (undefined_column): column q2.qbinders_id does not exist
在postgres中,联接表如下所示
CREATE TABLE qbook2qbinder
(
qbook_id uuid,
qbinder_id uuid,
qbook_order integer,
inserted_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL,
CONSTRAINT qbook2qbinder_qbinder_id_fkey FOREIGN KEY (qbinder_id)
REFERENCES qbinders (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT qbook2qbinder_qbook_id_fkey FOREIGN KEY (qbook_id)
REFERENCES qbooks (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
模型是
defmodule Project.Qbinders do
use Project.Web, :model
@primary_key {:id, :binary_id, autogenerate: true}
schema "qbinders" do
field :title, :string
field :reward, :string
many_to_many :qbooks, Project.Qbooks, join_through: "qbook2qbinder"
timestamps
end
为什么Ecto寻找qbinder_id而不是qbinder_id?如何将其设置为qbinder_id?这很容易通过的参数进行自定义 可以使用选项join_键配置列名,使用方式如下:
many_to_many :qbooks, Project.Qbooks, join_through: "qbook2qbinder",
join_keys: [{qbook_id: :id, qbinder_id: :id}]
我怀疑这是因为您的模型名是Qbinders,因此,列名是从该名称派生的。您应该能够配置自己与join_keys关键字参数的关联-请检查此点。谢谢如果你加上答案,我会接受的。很乐意帮忙!干杯
many_to_many :qbooks, Project.Qbooks, join_through: "qbook2qbinder",
join_keys: [{qbook_id: :id, qbinder_id: :id}]