我正在尝试让query =
from(i in SgListItems,
join: l in assoc(i, :sg_lists),
join: u in assoc(i, :sg_users),
select: [l.list_id, i.item_id]
)
使用Ecto工作。 Ecto似乎默认使用'id'作为连接条件的关键字段。尝试使用架构中的外键设置将其更改为list_id和user_id,但似乎不起作用。我想念什么?
SELECT s1."list_id", s0."item_id" FROM "sg_list_items" AS s0 INNER JOIN "sg_lists" AS s1 ON **s1."id"** = s0."list_id" INNER JOIN "sg_users" AS s2 ON **s2."id"** = s0."author_id" ) []
被翻译成
@primary_key {:list_id, :binary_id, autogenerate: true}
schema "sg_lists" do
field(:list_title, :string, null: false)
field(:list_description, :map, null: false)
has_many(:sg_list_items, MyApp.SgListItems, foreign_key: :list_id)
timestamps()
end
例如,sg_lists的架构如下:
@primary_key {:item_id, :binary_id, autogenerate: true}
schema "sg_list_items" do
belongs_to(:sg_lists, MyApp.SgLists, foreign_key: :list_id, type: :binary_id)
field(:item_hierarchy, :map, null: false)
field(:item_title, :string, null: false)
field(:item_description, :map, null: false)
belongs_to(:sg_users, MyApp.SgUsers, foreign_key: :author_id, type: :binary_id)
timestamps()
end
以下sg_list_items的架构。
1 error(s) occurred:
* module.cluster.module.cluster.azuread_application.cluster: 1 error(s) occurred:
* azuread_application.cluster: graphrbac.ApplicationsClient#Create: Failure responding to request: StatusCode=403 -- Original Error: autorest/azure: Service returned an error. Status=403 Code="Unknown" Message="Unknown service error" Details=[{"odata.error":{"code":"Authorization_RequestDenied","date":"2019-02-19T23:22:23","message":{"lang":"en","value":"Insufficient privileges to complete the operation."},"requestId":"<SOME GUID>"}}]
答案 0 :(得分:1)
您可以使用:on
的{{1}}选项来显式设置要加入的字段。
尝试类似的东西:
join
在这一行:query =
from(i in SgListItems,
join: l in assoc(i, :sg_lists), on: [id: i.list_id],
join: u in assoc(i, :sg_users), on: [id: i.user_id],
select: [l.list_id, i.item_id]
)
,[id: i.list_id]
指的是id
的{{1}},在下一行,它指的是:sg_lists
的
如果您要使用id
或:sg_users
以外的其他字段,则应放置这些字段而不是:sg_list
,并将:sg_users
替换为{{1 }}
这应该有效。
您甚至可以将多个条件添加到:id
选项中,例如:
i.list_id
也可以。但是我认为您所需要的就像上面的代码段一样。