Fix Ecto.Queryable protocol not implemented error

I am new to using Ecto and Elixir and I have encountered an error that I cannot explain. My code looks the same as in the example in Ecto README.

Here are my modules for 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

This is how I call the request function

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

All this is similar to the Ecto documentation, and I cannot find anything, to put it another way. But I get the following error.

protocol Ecto.Queryable not implemented for [%Ensalutilo.Registration{user_id: "underwater"}]
+4
source share
1 answer

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

, , , Ecto.Queryable Repo.all.

+6

Source: https://habr.com/ru/post/1569168/


All Articles