【问题标题】:Fix protocol Ecto.Queryable not implemented error修复协议 Ecto.Queryable 未实现错误
【发布时间】:2015-02-24 15:15:30
【问题描述】:

我刚开始使用 Ecto 和 Elixir,遇到了一个我无法解释的错误。我的代码看起来就像 Ecto README 中的示例。

这是我的 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"}]

【问题讨论】:

    标签: elixir phoenix-framework ecto


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-06-16
      • 2017-10-25
      • 2018-10-19
      • 2016-09-10
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-28
      相关资源
      最近更新 更多