Search⌘ K
AI Features

Delete Records With Associations

Explore how Ecto handles deleting records with associations in Elixir. Understand the on_delete options like :nothing, :nilify_all, and :delete_all to control child record behavior. Discover the impact of database-level constraints and learn how to manage deletions efficiently for robust database applications.

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 ...