我正在运行一个类似这样的查询:
query = from q in MY.Model, preload: [:accoc], where: q.accoc_field in [10, 11]
Repo.all(query)
这给了我一个错误:
** (Ecto.QueryError) iex:32: field `accoc_field` in `where` does not exist in schema MY.Model in query:
因此,基本上我尝试编写一个查询,如下所示:
select * from my_model m join assoc a on m.assoc_id = a.id where a.assoc_field in (10, 11)
我知道我可以在Ecto中使用joins来编写上述查询,但是在使用preload时我该怎么做呢?
发布于 2018-05-30 07:11:50
我希望这里最好的解决方案是直接跟随SQL代码前进。
我们需要连接另一个表,所以我们需要调用Ecto.Query.join/5函数,而不是预加载。
例如,假设我们需要检索用户,这些用户创建了具有特定部分的帖子:
from u in User,
join: p in assoc(u, :posts),
where: p.section in ["elixir", "phoenix"],
select: u
在此查询的Repo.all之后,我们将获得所需用户的列表。在这里,如果我们想用帖子填充用户-我们可以调用Repo.preload
https://stackoverflow.com/questions/50592611
复制相似问题