【发布时间】:2019-09-11 23:39:32
【问题描述】:
我目前有两个具有 hasMany 关系的表:
customers [hasMany] items
在items 内有一些belongsTo 关系:
providers [belongsTo] items
statuses [belongsTo] items
types [belongsTo] items
我的目标是有一个 Ecto 查询,它返回一个 customers 列表,其中至少有一个 items 记录,其中 status_id 为 2 以及该 customers 记录具有的任何其他 items。
我还需要预加载items、providers、statuses 和types
我在 SQL 中复制了如下:
SELECT
distinct on (c0.id)
c0."id",
c0."inserted_at",
c0."updated_at",
c1."id",
c1."inserted_at",
c1."updated_at"
FROM "customers" AS c0
INNER JOIN "items" AS c1 ON c1."customer_id" = c0."id"
WHERE EXISTS (
SELECT *
FROM "items" c2
WHERE c2."customer_id" = c0."id"
AND c2.status_id = 2);
这与我当前的 Ecto 查询有点逆向工程:
Repo.all(
from(p in Customers,
inner_join: r in assoc(p, :items),
where: r.status_id == 2,
preload: [
items: r, items: :provider, items: :type, items: :status
],
)
)
此 Ecto 查询为我提供了具有 status_id 为 2 的项目的客户,但它仅返回符合条件的项目记录。如果其中一条记录的 status_id 为 2,我希望拥有与该客户关联的所有项目。
我尝试通过使用子查询作为连接来实现这一点,如下所示:
Repo.all(
from(p in Customers,
inner_join: r in assoc(p, :items),
join: i in subquery(from(i in MyApp.Items, where: i.status_id == 2 )), on: p.id == i.id,
preload: [
items: r, items: :provider, items: :type, items: :status
]
)
)
但是,这不会返回任何结果。非常感谢 Ecto 专家的任何见解。
【问题讨论】: