Error: invalid or unknown type Ecto.Enum for field :status

Hey guys, I’m working with an Enum field on my schema, but I’m getting the following error:

(ArgumentError) invalid or unknown type Ecto.Enum for field :status

My migration and schema:

defmodule Livekanb.Repo.Migrations.CreateTasks do
  use Ecto.Migration

  def change do
    create table(:tasks, primary_key: false) do
      add :id, :binary_id, primary_key: true
      add :title, :string
      add :description, :string
      add :status, :string
      add :relator_user_id, references(:users, on_delete: :nothing, type: :binary_id)
      add :executor_user_id, references(:users, on_delete: :nothing, type: :binary_id)

      timestamps()
    end

    create index(:tasks, [:relator_user_id])
    create index(:tasks, [:executor_user_id])
  end
end

defmodule Livekb.Tasks.Task do
  use Ecto.Schema
  import Ecto.Changeset

  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id
  schema "tasks" do
    field :description, :string
    field :title, :string
    field :status, Ecto.Enum, values: [:todo, :doing, :done, :excluded]
    field :relator_user_id, :binary_id
    field :executor_user_id, :binary_id

    timestamps()
  end

  @doc false
  def changeset(task, attrs) do
    task
    |> cast(attrs, [:title, :description, :status])
    |> validate_required([:title, :description, :status])
  end
end

What am I doing wrong?

Do you have Ecto 3.5+? Ecto.Enum was added in 3.5.

4 Likes

Thank you for your reply. :slight_smile:

My mix deps.get shows ecto 3.7.1, but my mix.exs shows {:ecto_sql, “~> 3.4”}.

Do I need to update my mix.exs?

You could check in your mix.lock file.

BTW Your code looks good, maybe You can show your dependencies from mix.exs

1 Like

Also, would be nice to have your changeset, just to enforce you’re informing a valid value (atoms of the array).

2 Likes

Ecto.Enum already makes sure nothing besides the values in the list can be set. No need to manually validate that.

1 Like

Guys, thanks for replying. I generated a new project with an updated version of Phoenix and Ecto. It took a while for vscode to stop displaying the error message, but now it worked. :grinning_face_with_smiling_eyes:

1 Like

Makes sense. Thank you for the tip!