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

时间:2018-05-04 10:40:38

标签: elixir phoenix-framework ecto

我试图将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]}])

但是在这个查询中,我无法按照我想要的图像进行排序。有人可以帮我吗?

1 个答案:

答案 0 :(得分:5)

表达式

[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)]
相关问题