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

Building a custom query and nested association definitions

我正在尝试将 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)]