Ecto查询是否还包含没有has_many相关记录的记录?

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

我编写此查询来查找不在佛罗里达州的所有记录。

query =
  from papa in Papa,
    inner_join: account in assoc(papa, :account),
    inner_join: location in assoc(account, :locations),
    where: account.email == ^"[email protected]",
    where: papa.status == ^"active",
    where: location.papa_id == papa.id, <--- Some of these Papas have ZERO locations.
    where: location.state != ^"FL",
    group_by: [papa.id, location.state, account.id],
    distinct: [papa.id],
    select: [
      papa.id,
      papa.member_id,
      papa.full_name,
      account.id,
      account.full_name,
      location.state,
      papa.status
    ]

它正确返回了在佛罗里达州没有任何地点的所有Papa记录。不幸的是,它跳过了实际上没有位置记录的Papa记录。

如何在Ecto查询中也包含这些记录?

postgresql elixir ecto has-many
1个回答
0
投票

一旦您有

inner_join: location in assoc(account, :locations),

where子句

where: location.papa_id == papa.id

是多余的,因为它是通过加入assoc来暗示的。如果要包含[[0]]中的记录,则需要使用papas(有关不同联接返回的结果的可视化,请参见图left_join。)因此,摆脱left_join并将here的连接子句更改为:

where: location.papa_id == papa.id

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