我刚开始使用 Ecto 和 Elixir,并且遇到了一个无法解释的错误。我的代码看起来就像 Ecto 自述文件中的示例。
这是我的 Ecto 模型和查询模块
defmodule Registration do
use Ecto.Model
schema "registrations" do
field :user_id, :string
field :created_at, :datetime, default: Ecto.DateTime.local
field :updated_at, :datetime, default: Ecto.DateTime.local
end
end
defmodule RegistrationQuery do
import Ecto.Query
def by_user(user_id) do
query = from r in Registration,
where: r.user_id == ^user_id,
select: r
Repo.all(query)
end
end
这是我调用查询函数的方法
registrations = Repo.all RegistrationQuery.by_user("underwater")
这一切似乎与 Ecto 文档完全一致,我找不到任何其他说明。但我收到以下错误。
protocol Ecto.Queryable not implemented for [%Ensalutilo.Registration{user_id: "underwater"}]
Your by_user/1
函数已经在调用Repo.all
,所以当你打电话时registrations = Repo.all(...)
稍后,您将传递第一个的结果Repo.all
作为参数,它是一个列表,如您在错误消息中看到的那样!
需要明确的是,您收到此错误消息是因为您可以将任何实现 Ecto.Queryable 协议的内容传递到 Repo.all 中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)