Protocol.UndefinedError协议Ecto.Queryable

时间:2018-05-09 18:18:40

标签: elixir phoenix-framework ecto

我正在试图弄清楚如何对属于特定用户的记录进行查询范围。当我传入范围记录时,我收到此错误:

 ** (Protocol.UndefinedError) protocol Ecto.Queryable not implemented for [%Dreamhouse.Project{__meta__: #Ecto.Schema.Metadata<:loaded, "projects">, id: 878, inserted_at: ~N[2018-05-09 18:13:22.820266], rows: #Ecto.Association.NotLoaded<association :rows is not loaded>, title: "Test Title", updated_at: ~N[2018-05-09 18:13:22.820274], user: #Ecto.Association.NotLoaded<association :user is not loaded>, user_id: 1118}]. This protocol is implemented for: Atom, BitString, Ecto.Query, Ecto.SubQuery, Tuple

这是我的控制器代码:

user_id = Map.get(Dreamhouse.Guardian.Plug.current_resource(conn), :id)
user = Repo.get(User, user_id)

projects = Repo.all(from p in assoc(user, :projects))

query =
  from(
    p in projects,
    left_join: r in assoc(p, :rows),
    left_join: i in assoc(r, :images),
    order_by: r.index,
    order_by: i.index,
    preload: [rows: {r, images: i}]
  )

projects = Repo.all(query)

当我寻找p in projects时会发生这种情况。我看到它不喜欢我在列表中传递。但是为了创建查询会有什么期望?

1 个答案:

答案 0 :(得分:1)

Repo.all执行查询并返回结构列表。要将此作为另一个查询的源,您需要传递原始查询,而不是结构列表。删除Repo.all应解决此问题:

projects = from(p in assoc(user, :projects))