Wrapping Up: Make Multiple Changes with Transactions

Review what we have learned about making multiple changes with transactions.

What we have learned?

Ecto has excellent support for database transactions via the Repo.transaction function.

  • The last lesson explained how using Ecto.Multi is preferable to using functions. However, cases do exist where calling transaction with a function works well.
  • If we’re only running a small number of operations and don’t need to take different actions depending on which operation succeeds or fails, using a function is a good option.
  • We should consider Ecto.Multi for all other cases. It has more flexibility and the code 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, and group operations together.

What we’ll learn next

We’ve worked through numerous examples based on the dataset included with the MusicDB sample application. For our last stop, we’ll look at how that dataset got created in the first place. The Ecto.Migration module provides tools to set up our database tables from within our Elixir code and make changes to them over the life of your application. With this last piece of the puzzle, we’ll be ready to create your application and put our Ecto knowledge to work.

Get hands-on with 1200+ tech skills courses.