Deleting Records with Associations
An important thing to consider with associations is what should happen to child records when a parent record is deleted. For example, if we delete an album, what should we do with the associated tracks? Ecto provides the on_delete: option to specify the desired behavior, but the exact implementation will vary depending on the database you’re using. Let’s take a closer look.
One approach is to define the behavior when creating the association. The has_many, has_one, and many_to_many functions all support the on_delete: option, which can have one of three values:
-
:nothing—This is the default behavior: Ecto won’t try to do anything to the child records if the parent is deleted.
-
:nilify_all—Ecto updates all ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access