Wrapping Up
Ecto has excellent support for database transactions via the Repo.transaction function. The last section covered some of the ways that using Ecto.Multi is preferable to using functions, but cases definitely exist where calling transaction with a function works well.
If you’re only running a small number of operations and you don’t need to take different action depending on which operation succeeds or fails, using a function is a good option. For all other cases, you should consider Ecto.Multi. It has a lot more flexibility, and the code needed to respond to different types of errors will be much cleaner and easier to follow.
We’ve almost completed our tour of Ecto. We’ve looked at how to run queries, make changes, map records to structs, ...
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