【发布时间】:2019-03-16 16:35:37
【问题描述】:
我认为 Elixir 中有一个相对简单的 Ecto 查询,它抛出一个错误“协议未实现”,我很困惑为什么。
所以这里是查询:
head_children_list = Comments
|> where([c], c.inserted_at in ^head_children)
|> Enum.sort_by(& &1["votetotal"])
所以 head_children 是一个包含一个字符串时间戳值的列表。这是确认的终端输出:
value of head_children
["2018-10-11 14:08:15.021033"]
所以我知道一个事实,这是一个列表。但是,当我尝试执行上述查询时,我得到以下信息:
[info] Sent 500 in 168ms
[error] #PID<0.401.0> running AlbatrossWeb.Endpoint (cowboy_protocol) terminated
Server: localhost:4000 (http)
Request: POST /voteComment
** (exit) an exception was raised:
** (Protocol.UndefinedError) protocol Enumerable not implemented for #Ecto.Query<from c in Albatross.Comments, where: c.inserted_at in ^[["2018-10-11 14:08:15.021033"]]>. This protocol is implemented for: DBConnection.PrepareStream, DBConnection.Stream, Date.Range, Ecto.Adapters.SQL.Stream, File.Stream, Function, GenEvent.Stream, HashDict, HashSet, IO.Stream, List, Map, MapSet, Postgrex.Stream, Range, Stream
请注意,这表示为列表实现了协议。但这是一个清单!所以我很困惑。我试过使用^[head_children],它只是将它包装在另一个列表中,但这也失败并出现同样的错误。
有人知道怎么回事吗?
【问题讨论】:
-
旁注:最好在数据库中排序。
标签: postgresql list elixir phoenix-framework ecto