【问题标题】:Building a custom query and nested association definitions构建自定义查询和嵌套关联定义
【发布时间】:2018-05-04 18:52:25
【问题描述】:

我正在尝试将 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)]?
  • 是的,就是这样。谢谢!我认为这种情况下的括号是可选的?我猜不是。
  • 它们通常是可选的,但由于我在回答中描述的模棱两可,这里是必需的。

标签: elixir phoenix-framework ecto


【解决方案1】:

表达式

[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)]

【讨论】:

    猜你喜欢
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    • 2013-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多