Search code examples
elixirphoenix-frameworkecto

Ecto Queryable not implemented for Phoenix module


I'm following the Programming Phoenix 1.4 tutorial and have run into a problem where I'm being told Ecto Queryable is not implemented for a given module but I can't tell why. There's another question similar to this on SO but it has a simple answer that does not apply to my case.

I have this code in my lib/rumbl/accounts/user.ex file:

defmodule Rumbl.Accounts.User do
  use Ecto.Schema
  import Ecto.Changeset

  schema "users" do
    field :name, :string
    field :username, :string

    timestamps()
  end
end

This is the code in my lib/rumbl/accounts/accounts.ex file:

defmodule Rumbl.Accounts do
  @moduledoc """
  The Accounts context.
  """

  alias Rumbl.Accounts.User
  alias Rumbl.Repo

  def get_user(id) do
    Repo.get(User, id)
  end

  def get_user!(id) do
    Repo.get!(User, id)
  end

  def get_user_by(params) do
    Repo.get_by(User, params)
  end

  def list_users do
    Repo.all(User)
  end
end

However, when I call Rumbl.Accounts.list_users from my UserController, I get the following error:

** (Protocol.UndefinedError) protocol Ecto.Queryable not implemented for Rumbl.Accounts.User, the given module does not provide a schema. This protocol is implemented for: Atom, BitString, Ecto.Query, Ecto.SubQuery, Tuple

I don't get the same error when I run the same queries in mix -S iex.

This may be an error in the code the book suggests since it's in beta, but I can't for the life of me figure out what was wrong. In the other SO post the user had forgotten to alias Rumbl.Accounts.User, but that's clearly not the case for me.


Solution

  • Your code is correct and should work without issues. The problem may be solved by cleaning the build files and recompiling your code:

    $ mix clean
    $ mix compile