Search code examples
elixirphoenix-framework

Phoenix - Problem when get model with two custom primary key


I have one model like this:

  @schema_prefix "sqlmgr"
  @derive {Phoenix.Param, key: :field_a}

  @primary_key false
  schema "table_a" do
    field :field_a, :string, size: 30, primary_key: true
    field :field_b, :string, size: 20, primary_key: true
    field :field_c, :string, size: 100

I get the error Ecto.Repo.get/2 requires the schema HttpApi.TableAs.TableA to have exactly one primary key, got: [:field_a, :field_b] when I access :show endpoint


Solution

  • As by your snippet, even despite @derive you have two primary keys explicitly defined. This is impossible, as you were told by the error message. Remove one of those and it’ll work.

    #                                  ⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓
    field :field_a, :string, size: 30, primary_key: true
    field :field_b, :string, size: 20, primary_key: true