Search code examples
elixirphoenix-frameworkecto

Ecto: column v0.availabilites does not exist


One of my actions fails with

(Postgrex.Error) ERROR (undefined_column): column v0.availabilites does not exist

Looking in the Phoenix logs, I found the query that generated the error:

[debug] SELECT v0.id, v0.event_id, v0.availabilites, v0.inserted_at, v0.updated_at
FROM votes AS v0 WHERE (v0.event_id IN ($1)) ORDER BY v0.event_id [1] ERROR query=185.8ms

The absurd thing is that the mentioned column exists! Here is the output of psql:

    Table "public.votes"
     Column     |            Type             |                     Modifiers                      
----------------+-----------------------------+----------------------------------------------------
 id             | integer                     | not null default nextval(votes_id_seq::regclass)
 availabilities | jsonb                       | 
 inserted_at    | timestamp without time zone | not null
 updated_at     | timestamp without time zone | not null
 event_id       | integer                     | 
Indexes:
    "votes_pkey" PRIMARY KEY, btree (id)
    "votes_event_id_index" btree (event_id)
Foreign-key constraints:
    "votes_event_id_fkey" FOREIGN KEY (event_id) REFERENCES events(id) ON DELETE CASCADE

As you can see, the column is there! Here are the two relevant models:

defmodule LetsPlan.Vote do
  use LetsPlan.Web, :model

  schema "votes" do
    belongs_to :event, LetsPlan.Event
    embeds_many :availabilites, LetsPlan.Availability

    timestamps
  end

  @required_fields ~w(availabilites event_id)
  @optional_fields ~w()

  def changeset(model, params \\ :empty) do
    model
    |> cast(params, @required_fields, @optional_fields)
  end
end

defmodule LetsPlan.Availability do
  use LetsPlan.Web, :model

  embedded_schema do
    field :from, Ecto.Date
    field :to, Ecto.Date
  end
end

Solution

  • You have a typo:

    embeds_many :availabilites, LetsPlan.Availability
    

    Should be:

    embeds_many :availabilities, LetsPlan.Availability