修复协议Ecto.Queryable未实现错误 [英] Fix protocol Ecto.Queryable not implemented error

查看:76
本文介绍了修复协议Ecto.Queryable未实现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用Ecto和Elixir的新手,遇到了一个我无法解释的错误.我的代码看起来像Ecto README中的示例.

I'm new to using Ecto and Elixir and I've come across an error that I can't explain. My code looks just like the example in the Ecto README.

这是我的Ecto模型和查询模块

Here are my modules for the Ecto Model and 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

这是我如何调用查询功能

Here is how I call the query function

registrations = Repo.all RegistrationQuery.by_user("underwater")

这一切似乎与Ecto文档完全一致,而且我找不到其他说明.但出现以下错误.

This all seems exactly in line with the Ecto documentation, and I can't find anything saying otherwise. But I get the following error.

protocol Ecto.Queryable not implemented for [%Ensalutilo.Registration{user_id: "underwater"}]

推荐答案

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

Your by_user/1 function is already calling Repo.all, so when you call registrations = Repo.all(...) later on, you are passing the result of the first Repo.all as argument, which is a list as you see in the error message!

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

To be clear, you get this error message because you can pass anything that implements the Ecto.Queryable protocol into Repo.all.

这篇关于修复协议Ecto.Queryable未实现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆