Search code examples
elixirphoenix-frameworkecto

Ecto.Queryable not implemented for User / Phoenix Error


Following along with the Programming Phoenix 1.4 book. The module worked fine when I was using a manual database, but when I tried to switch my accounts.ex file (like the book told me to do), it broke:

defmodule Rumbl.Accounts do
  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

Here's the full error I get when I changed my account.ex file:

[error] #PID<0.732.0> running RumblWeb.Endpoint (connection #PID<0.731.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: GET /users
** (exit) an exception was raised:
    ** (Protocol.UndefinedError) protocol Ecto.Queryable not implemented for User, the given module does not exist. This protocol is implemented for: Atom, BitString, Ecto.Query, Ecto.SubQuery, Tuple
        (ecto) lib/ecto/queryable.ex:40: Ecto.Queryable.Atom.to_query/1
        (ecto) lib/ecto/repo/queryable.ex:14: Ecto.Repo.Queryable.all/3
        (rumbl) lib/rumbl_web/controllers/user_controller.ex:7: RumblWeb.UserController.index/2
        (rumbl) lib/rumbl_web/controllers/user_controller.ex:1: RumblWeb.UserController.action/2
        (rumbl) lib/rumbl_web/controllers/user_controller.ex:1: RumblWeb.UserController.phoenix_controller_pipeline/2
        (rumbl) lib/rumbl_web/endpoint.ex:1: RumblWeb.Endpoint.instrument/4
        (phoenix) lib/phoenix/router.ex:275: Phoenix.Router.__call__/1
        (rumbl) lib/rumbl_web/endpoint.ex:1: RumblWeb.Endpoint.plug_builder_call/2
        (rumbl) lib/plug/debugger.ex:122: RumblWeb.Endpoint."call (overridable 3)"/2
        (rumbl) lib/rumbl_web/endpoint.ex:1: RumblWeb.Endpoint.call/2
        (phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:34: Phoenix.Endpoint.Cowboy2Handler.init/2

Solution

  • Ecto.Queryable not implemented for User, the given module does not exist.

    The error is pretty clear, meaning you don't have a module called User (it's probably Rumbl.User or Rumbl.Accounts.User in your application).

    Assuming you have indeed created the ecto schema Rumbl.User, you should:

    1. Either alias it in the module you use:

      alias Rumbl.User
      
    2. or use the fully-qualified name of the module:

      Repo.get(Rumbl.User, id)