Repo.all(Container) error

I have this schema:

defmodule MyP.Container do
  use MyP.Web, :model

  schema "container" do
    field :container_ID, :integer
    field :container_name, :string
    field :container_category, :string
  end

  @primary_key {:container_ID}
  @derive {Phoenix.Param, key: :container_ID}
  @required_fields ~w(container_ID container_name container_category)a

  @doc """
  Builds a changeset based on the `struct` and `params`.
  """
  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, @required_fields)
    |> validate_required(@required_fields)
  end
end

and this migration:

defmodule MyP.Repo.Migrations.CreateContainer do
  use Ecto.Migration

  def change do
    create table(:container, primary_key: false) do
      add :container_ID, :integer, size: 5, primary_key: true, autogenerate: true, null: false
      add :container_name, :string, size: 50, null: false
      add :container_category, :string, size: 45, null: false
    end

    create unique_index(:container, [:container_name])
  end
end

when I enter via: iex -S mix and query the container repo [Repo.all(Container)] I got this error:

** (Mariaex.Error) (1054): Unknown column 'c0.id' in 'field list'

But why did keeps occuring if the field id doesn’t exist in my table (or I think so)

1 Like

You need to set @primary_key before defining the schema.

1 Like