修复协议Ecto.Queryable未实现的错误。

4

我是一个新手,正在学习使用Ecto和Elixir,但我遇到了一个错误,无法解释。我的代码看起来就像Ecto README中的示例一样。

下面是我的Ecto Model和Query模块:

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"}]
1个回答

6

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接