How To Delete Many-To-Many Relation In Elixir Ecto — Tentamen Software Testing Blog

Image for post
Image for post
Elixir Ecto Many To Many Relation

TL;DR

Deleting Many-To-Many Relation in Elixir Ecto

The Wrong Way

user
|> User.changeset_update_projects(projects)
|> Repo.update()

but that also deleted a User record.

Then we found this excellent advice on the Elixir forum:

Repo.delete/2 expects a changeset_or_struct as the first argument. It looks like you’re piping an #Ecto.Query ( ie.queryable) into it. I believe delete_all/2 66 is what you’re looking for.

without the query implementation.

The Query For Deleting Many-To-Many Relation In Elixir Ecto

https://gist.github.com/karlosmid/c4b6abb224efa563592d56dc00d5efec

Originally published at https://blog.tentamen.eu on September 14, 2020.

Written by

Founder of Tentamen, software testing agency.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store