Protocol.UndefinedError 协议 Ecto.Queryable
Protocol.UndefinedError protocol Ecto.Queryable
我想弄清楚如何确定属于特定用户的记录的查询范围。当我传入范围记录时,出现此错误:
** (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
时会发生这种情况。我看到它不喜欢我传递一个列表。但是为了创建查询它期望什么?
Repo.all
执行查询和 returns 结构列表。要将其用作另一个查询的源,您需要传递原始查询,而不是结构列表。删除 Repo.all
应该可以解决这个问题:
projects = from(p in assoc(user, :projects))
我想弄清楚如何确定属于特定用户的记录的查询范围。当我传入范围记录时,出现此错误:
** (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
时会发生这种情况。我看到它不喜欢我传递一个列表。但是为了创建查询它期望什么?
Repo.all
执行查询和 returns 结构列表。要将其用作另一个查询的源,您需要传递原始查询,而不是结构列表。删除 Repo.all
应该可以解决这个问题:
projects = from(p in assoc(user, :projects))