Delete Records With Associations

Learn how to delete records with associations.

What happens when we delete a record?

An important thing to consider with associations is what should happen to child records when a parent record is deleted. For example, what should we do with the associated tracks if we delete an album? Ecto provides the on_delete: option to specify the desired behavior, but the exact implementation will vary depending on the database we’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. They can have one of three values, as shown in figure:

Get hands-on with 1200+ tech skills courses.