How to remove the associated entry if not cast into the changeset?

When I edit schema A that has_many schema B.

and then do not send the associated fields of schema B, it does not get deleted. How to delete the associated field?

Can You please provide enough information for people to understand how to help You?

At least some code of what You have tried, and why it fails…

All of the associations could be deleted when replacing, simply by setting on_replace option when declaring schema. If you did not set this option and you have tried to update the database record then it means that you did not replaced associated fields as otherwise the default on_replace behaviour is set to :raise. Also ecto would never remove data from target association when using many_to_many.

For more information see:

  1. The :on_replace option section at Ecto.Changeset documentation
  2. Removing data section at Ecto.Schema documentation
1 Like

If this is using an HTML form make sure it‘s actually deleting the associations and not sending no change, as form encoding cannot represent an empty list.