4
votes

It's not clear to me how to delete a many to many association in Ecto. I do not want to delete the associated structures, just the relationship.

My first attempt was to find the association in join_through table and delete it, but there is no primary key so Repo.delete fails.

1
you can define a :on_delete option that specifies which action should be performed on associations when the parent is deleted: Ecto docs has_many/3Pascal
I don't want to delete the parent. I want to delete the association itselfFletcher Moore
Have you tried Repo.delete_all/2 with a query on the join table and both foreign keys?Martin Svalin
That worked Martin. Thank you.Fletcher Moore

1 Answers

3
votes

Repo.delete_all/2 with a query on the join table and both foreign keys.

https://hexdocs.pm/ecto/Ecto.Repo.html#c:delete_all/2