0
votes

Good morning,

Starting out with Phoenix and Elixir and we are having issues with the online tutorial when using Ecto with Phoenix V1.3.2.

We've setup the application with the following

mix phx.new api --no-brunch --no-html --database=mysql

Following the tutorial we get to using Ecto, however we've read that there's some breaking changes but cannot figure out the reason for the below error.

** (exit) an exception was raised: ** (Protocol.UndefinedError) protocol Ecto.Queryable not implemented for Api.User, the given module does not exist. This protocol is implemented for: Atom, BitString, Ecto.Query, Ecto.SubQuery, Tuple

Here is some example code for one of our controllers which gets invoked fine (api/lib/api_web/v1/controllers

defmodule ApiWeb.AuthenticationController do
  use ApiWeb, :controller

  def auth(conn, %{"username" => username, "password" => password}) do
    json conn, Api.Repo.all(Api.User) # ??
  end
end

and our Ecto Schema here generated by mix.gen.model (api/web/models)

defmodule Api.User do
  use Api.Web, :model

  schema "users" do
    field :username, :string
    field :display_name, :string
    field :password, :binary
    field :api_key, :binary
    field :api_expiry, :naive_datetime
    field :company, :string
    field :email, :string

    timestamps()
  end

  @doc """
  Builds a changeset based on the `struct` and `params`.
  """
  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:username, :display_name, :password, :api_key, :api_expiry, :company, :email])
    |> validate_required([:username, :display_name, :password, :api_key, :api_expiry, :company, :email])
  end
end

We have not ventured too far into the Elixir & Phoenix world hence there is no business logic, simply following the online tutorial for now.

Thanks in advance,

1

1 Answers

1
votes

Api.User should implement Ecto.Queryable protocol, what is clearly stated by the error message. While might do that manually, the common approach would be to use Ecto helpers to accomplish this task. Usually, the following two imports and use are required to cover all the needs:

use Ecto.Schema
import Ecto.Changeset
import Ecto.Query

That said, just add these three lines to your Api.User module:

defmodule Api.User do
  use Ecto.Schema
  import Ecto.Changeset
  import Ecto.Query

  ...
end