使用 order_by 预加载所有关联
Preload all associations with an order_by
这就是我想要做的:
project =
Repo.get!(Project, id)
|> Repo.preload([rows: from(r in Row, order_by: r.index)] [images: from(i in Image, order_by: i.index)])
但是我收到这个错误:
** (ArgumentError) the Access calls for keywords expect the key to be an atom, got: [images: #Ecto.Query<from i in Dreamhouse.Image, order_by: [asc: i.index]>]
有人知道我做错了什么吗?
我怀疑我能否解释错误信息,但原因是:
[rows: from(...)] [images: from(...)]
不是有效的 Elixir。检查:
[[foo: 42] [bar: 3.14]]
应该是:
[rows: from(...), images: from(...)]
错误消息说,您正在尝试从关键字列表中获取元素,但传递的是意外的东西而不是原子。
例如,以下代码会产生类似的错误:
keyword_list = [keyword: :argument]
keyword_list[unexpected: :structure]
所以问题出在这一行:
Repo.preload([rows: from(r in Row, order_by: r.index)] [images: from(i in Image, order_by: i.index)])
您可能希望提供一个关键字列表作为您要预加载的所有关联的参数:
Repo.preload([rows: from(r in Row, order_by: r.index), images: from(i in Image, order_by: i.index)])
这就是我想要做的:
project =
Repo.get!(Project, id)
|> Repo.preload([rows: from(r in Row, order_by: r.index)] [images: from(i in Image, order_by: i.index)])
但是我收到这个错误:
** (ArgumentError) the Access calls for keywords expect the key to be an atom, got: [images: #Ecto.Query<from i in Dreamhouse.Image, order_by: [asc: i.index]>]
有人知道我做错了什么吗?
我怀疑我能否解释错误信息,但原因是:
[rows: from(...)] [images: from(...)]
不是有效的 Elixir。检查:
[[foo: 42] [bar: 3.14]]
应该是:
[rows: from(...), images: from(...)]
错误消息说,您正在尝试从关键字列表中获取元素,但传递的是意外的东西而不是原子。
例如,以下代码会产生类似的错误:
keyword_list = [keyword: :argument]
keyword_list[unexpected: :structure]
所以问题出在这一行:
Repo.preload([rows: from(r in Row, order_by: r.index)] [images: from(i in Image, order_by: i.index)])
您可能希望提供一个关键字列表作为您要预加载的所有关联的参数:
Repo.preload([rows: from(r in Row, order_by: r.index), images: from(i in Image, order_by: i.index)])