This repository is a self-learning and educational resource for working with SQLAlchemy in asynchronous Python environments. It covers all major parts of SQLAlchemy including Core, ORM, Async engine, and Alembic for migrations. It also demonstrates how to test async database code using pytest.
- Setting up AsyncEngine with
asyncpg - Connecting and querying using Core syntax
- Using the ORM with async sessions and models
- Executing queries using both Core and ORM in an async context
Each query is implemented both in Core and ORM styles:
SELECT,INSERT,UPDATE,DELETE- Filtering, ordering, and pagination
- Transactions and session management
- Aggregate functions like
COUNT(),SUM(),AVG(),MIN(),MAX()
❗ Note: This repo does not cover many-to-many relationships.
- Using
pytestwithpytest-asyncio - Creating a
conftest.pyto manage test DB sessions - One working async test included as an example
- Alembic initialized and configured for async SQLAlchemy
- Commands to autogenerate and apply migrations
- Versioned migrations for schema tracking
- Python 3.8+
- SQLAlchemy 2.x (with async support)
- PostgreSQL (or any compatible async-supported DB)
- asyncpg (DB driver)
- Alembic
- Pytest + pytest-asyncio