构建自定义查询和嵌套关联定义



我正在尝试将has_many关联预加载到传入的结构和传递的结构关联的has_many关联。

这是我现在正在使用的内容:

project =
Repo.get!(Project, id)
|> Repo.preload([rows: {query, [images: from i in Image, order_by: i.index]}])

但这会返回此错误:

This error may happen when you forget a comma in a list or other container:
[a, b c, d]
Elixir cannot compile otherwise. Syntax error before: ','

不过,这工作得很好:

project =
Repo.get!(Project, id)
|> Repo.preload([rows: {query, [:images]}])

但是在此查询中,我无法对图像进行所需的排序。有人可以帮我解决这个问题吗?

表达式

[images: from i in Image, order_by: i.index]

由于逗号而模棱两可。它可以解释为:

[images: from(i in Image, order_by: i.index)]

或作为:

[images: from(i in Image), order_by: i.index]

您需要添加明确的括号来解决歧义。在这种情况下,您需要:

[images: from(i in Image, order_by: i.index)]

最新更新