Transactions and Ecto.Multi
Group multiple database operations into atomic units using Repo transactions and the composable Ecto.Multi struct.
Why Transactions?
A transaction bundles several operations so they all succeed or all roll back together, keeping data consistent.
Repo.transaction with a Function
The simplest form runs a function; if it returns normally the transaction commits, if it raises it rolls back.
Repo.transaction(fn ->
Repo.insert!(%Account{balance: 100})
Repo.insert!(%Log{action: "created"})
end)All lessons in this course
- Ecto Schema and Database Migrations
- Repo, Changesets, and Queries
- Associations and Embedded Schemas
- Transactions and Ecto.Multi