No_assoc_constraint does not work

Hi,
i am trying to follow the Programming Phoenix book and have got the videos and categories table setup with association setup.

I am able to get foreign_key_constraint to work. But not no_assoc_constraint. Can someone give me some clue why this is so? Maybe i understood this constraint wrongly?

iex(43)> category |> Ecto.Changeset.change |> Ecto.Changeset.foreign_key_constraint(:videos, name:
 :videos_category_id_fkey, message: "still exist") |> Repo.delete
[debug] QUERY ERROR db=1.0ms
DELETE FROM "categories" WHERE "id" = $1 [2]
{:error,
 #Ecto.Changeset<action: :delete, changes: %{},
  errors: [videos: {"still exist", []}], data: #Rumbl.Category<>,
  valid?: false>}
iex(43)> category |> Ecto.Changeset.change |> Ecto.Changeset.no_assoc_constraint(:videos) |> Repo.
delete
** (ArgumentError) cannot add constraint to changeset because association `videos` does not exist
    (ecto) lib/ecto/changeset.ex:2089: Ecto.Changeset.get_assoc/2
    (ecto) lib/ecto/changeset.ex:2031: Ecto.Changeset.no_assoc_constraint/3

no_assoc_constraint only works on has_* (https://hexdocs.pm/ecto/Ecto.Changeset.html#no_assoc_constraint/3) - does the categories schema have the has_many :videos association?

Brilliant! thanks for pointing this out, have been scratching my head for hours over this

1 Like