https://github.com/nounder/250115-sqlalchemy-wo-autobegin-tx
https://github.com/nounder/250115-sqlalchemy-wo-autobegin-tx
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/nounder/250115-sqlalchemy-wo-autobegin-tx
- Owner: nounder
- Created: 2025-01-15T19:19:58.000Z (4 months ago)
- Default Branch: master
- Last Pushed: 2025-01-15T19:29:15.000Z (4 months ago)
- Last Synced: 2025-03-06T03:13:33.524Z (3 months ago)
- Language: Python
- Size: 2.93 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Disable SQLAlchemy autobegin function that wraps each operation in transaction.
This enables to operate on Postgres DDL, like creating table indexes.
See `main.py` for source code.
Run `docker compose up` for reproducible example.
Add `--watch` for automatic restart on source change.
Run `docker compose logs app` to narrow down to python logs.The sauce is `isolation_level="AUTOCOMMIT"`. It tell underlying Postgres library to
commit on each statement. SQLAlchemy knows it so it doesn't autobegin transactions.
See more in [SQLALchemy docs][0]. Note that solution will not work if you want to create mutliple
indexes in one transaction. If you wanna do it your best bet is to use DBAPI directly.[0]: https://docs.sqlalchemy.org/en/20/core/connections.html#setting-transaction-isolation-levels-including-dbapi-autocommit