且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何查询Ecto关联中的第三个关系

更新时间:2023-01-31 11:15:19

您正在预加载吨,这将为您提供大量数据,您可以在代码中过滤我们的数据,但是如果您使用 join / 5

You're preloading a ton, which will get you lots of data you could filter our in code, but the database can return much less data if you use join/5:

假设我已经解释了您的方案正确,这就是我的工作。

Assuming I've interpreted your scheme correctly, here's what I'd do.

import Ecto.Query

query =
  from s in MyApp.Snapmail,
  join: sc in assoc(s, :snapmail_cameras),
  join: c in assoc(sc, :camera),
  where: c.status != ^"status_finished"

MyApp.Repo.all(query)

这只会返回 Snapmails 而没有所有这些预加载,但是您始终可以添加`preload / 3'在查询结束时将数据导入。

This will only return Snapmails without all those preloads, but you can always add a `preload/3' at the end of your query to bring that data in.