A transaction wraps a connection with both a session ID and a transaction ID, scoping all operations to a single atomic unit. Changes are applied only when the transaction is committed, and can be rolled back with cancel.
Transactions are created by calling .begin_transaction() on a session. They are not available on HTTP or embedded connections.
Note
Source: async_ws.py · blocking_ws.py
Creating a transaction
Returns (sync): BlockingSurrealTransaction
Returns (async): AsyncSurrealTransaction
Examples
Inherited methods
A transaction exposes query and CRUD methods that mirror the parent connection's interface. All operations are scoped to this transaction and are not visible outside it until committed. For full parameter tables and examples, see the Surreal reference.
| Method | Returns | Description |
|---|---|---|
.query(query, vars) | Awaitable / lazy Value or tuple[Value, ...] builder | Execute one or more SurrealQL statements within the transaction. |
.select(record) | Value | Select records. |
.create(record, data) | CRUD builder -> dict[str, Value] | Create a record (chain .content/.replace/.merge/.patch). |
.update(record, data) | CRUD builder -> dict or list | Update records (chain .content/.replace/.merge/.patch). |
.upsert(record, data) | CRUD builder -> dict or list | Upsert a record (chain .content/.replace/.merge/.patch). |
.delete(record) | CRUD builder -> dict or list | Delete records. |
.insert(table, data, relation=False) | Insert builder -> list[Value] | Insert records. Pass relation=True or chain .relation() for INSERT RELATION. |
.run(name, args, version) | Value | Call a SurrealDB function within the transaction. |
.let(key, value) | None | Set a transaction-scoped variable. |
.unset(key) | None | Unset a transaction-scoped variable. |
Transaction-specific methods
.commit()
Commits the transaction, applying all changes made within it to the database. After committing, the transaction object should not be used.
Returns: None
Examples
.cancel()
Cancels the transaction, discarding all changes made within it. No data is written to the database. After cancelling, the transaction object should not be used.
Returns: None
Examples
Complete example
See also
SurrealSession — Session management reference
Surreal — Connection reference with full method documentation
Data Types — Type aliases and value types
Errors — Error classes reference