Ecto:列 v0.availabilites 不存在

Ecto: column v0.availabilites does not exist

我的一个操作失败了

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

查看 Phoenix 日志,我发现产生错误的查询:

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

荒谬的是,提到的专栏存在!这是 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

如您所见,专栏就在那里!以下是两个相关模型:

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

你打错了:

embeds_many :availabilites, LetsPlan.Availability

应该是:

embeds_many :availabilities, LetsPlan.Availability