修复协议 Ecto.可查询未实现错误



我是使用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"}]

您的by_user/1函数已经在调用Repo.all,因此当您稍后调用registrations = Repo.all(...)时,您将第一个Repo.all的结果作为参数传递,这是您在错误消息中看到的列表!

需要明确的是,您会收到此错误消息,因为您可以将实现 Ecto.Queryable 协议的任何内容传递到 Repo.all 中。

最新更新