transaction and Transaction¶
transaction() is an async context manager that runs everything inside the block on one connection, committing on exit and rolling back on exception. It yields a Transaction handle for raw SQL that must run on the transaction's connection. Route it with using=, an explicit session=, or the ambient session (ferro.engines.session(...)). See the Transactions guide for semantics and patterns.
transaction
async
¶
Run the operations in the block in one database transaction.
The transaction commits when the block exits and rolls back when it
raises. A transaction() inside another one opens a savepoint. The
yielded Transaction handle runs raw SQL on the
transaction's connection, for statements no model covers, such as
setting a Postgres setting or taking an advisory lock. See
Transactions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
using
|
str | None
|
The named connection to open the transaction on. Defaults to
|
None
|
session
|
Session | None
|
The session to open the transaction in. Defaults to |
None
|
immediate
|
bool
|
On SQLite, open with |
False
|
Yields:
| Name | Type | Description |
|---|---|---|
tx |
Transaction
|
A handle with |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
No session, |
Examples:
A failure inside the block rolls back every write in it:
from ferro import transaction
try:
async with transaction():
await Author.create(name="Mary Somerville", email="mary@example.com")
raise RuntimeError("abandon the import")
except RuntimeError:
pass
assert await Author.where(lambda author: author.name == "Mary Somerville").count() == 0
The handle runs raw SQL in the same transaction:
from ferro import transaction
async with transaction() as tx:
await Author.create(name="Mary Somerville", email="mary@example.com")
row = await tx.fetch_one("SELECT count(*) AS total FROM author")
assert row == {"total": 3}
Transaction
¶
The handle async with transaction() as tx yields, for raw SQL on its connection.
Its methods take the same sql and *args as the top-level
execute, fetch_all and
fetch_one, and always run on the transaction's
connection. After the block exits, every call raises RuntimeError.
Examples:
from ferro import transaction
async with transaction() as tx:
await tx.execute('UPDATE "post" SET "views" = 0 WHERE "id" = ?', 1)
row = await tx.fetch_one('SELECT "views" FROM "post" WHERE "id" = ?', 1)
assert row == {"views": 0}
-
Reference
Python API
transaction and Transaction
transaction
-
Reference
Python API
transaction and Transaction
transaction
Methods:
| Name | Description |
|---|---|
execute |
Run one SQL statement on this transaction's connection. |
fetch_all |
Run one SQL query on this transaction's connection and return every row. |
fetch_one |
Run one SQL query on this transaction's connection and return its first row. |
execute
async
¶
Run one SQL statement on this transaction's connection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql
|
str
|
One SQL statement, with |
required |
*args
|
Any
|
The values bound to the placeholders, in order. |
()
|
Returns:
| Type | Description |
|---|---|
int
|
The number of rows the statement changed. |
Raises:
| Type | Description |
|---|---|
ValueError
|
|
TypeError
|
A value in |
RuntimeError
|
The transaction's block has exited. |
fetch_all
async
¶
Run one SQL query on this transaction's connection and return every row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql
|
str
|
One SQL query, with |
required |
*args
|
Any
|
The values bound to the placeholders, in order. |
()
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
One dict per row, keyed by column name. |
Raises:
| Type | Description |
|---|---|
ValueError
|
|
TypeError
|
A value in |
RuntimeError
|
The transaction's block has exited. |
fetch_one
async
¶
Run one SQL query on this transaction's connection and return its first row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql
|
str
|
One SQL query, with |
required |
*args
|
Any
|
The values bound to the placeholders, in order. |
()
|
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
The first row, keyed by column name, or |
Raises:
| Type | Description |
|---|---|
ValueError
|
|
TypeError
|
A value in |
RuntimeError
|
The transaction's block has exited. |