Skip to content

sqlalchemy ​

SQLAlchemy ORM with async support and Alembic migrations.


When to use it ​

Choose sqlalchemy when your project needs:

  • An ORM with async PostgreSQL support via SQLAlchemy
  • Database migrations managed by Alembic
  • A DeclarativeBase for your models
  • An async session factory and get_session FastAPI dependency

What it adds ​

Files ​

FilePurpose
my_project/db/__init__.pyPackage marker
my_project/db/base.pySQLAlchemy DeclarativeBase
my_project/db/session.pycreate_async_engine, async_sessionmaker, get_session dependency
my_project/models/__init__.pyImports all models for Alembic discovery
my_project/models/mixins.pyTimestampMixin with created_at and updated_at
alembic.iniAlembic configuration
alembic/env.pyAlembic environment wired to async SQLAlchemy
alembic/script.py.makoMigration script template

Code injections ​

PointTargetWhat it adds
lifespan_importslifecycle.pyfrom .db.session import engine
lifespan_shutdownlifecycle.pyawait engine.dispose()

Dependencies ​

PackagePurpose
sqlalchemy[asyncio]ORM with async support
alembicDatabase migrations

Dev dependencies: aiosqlite (for testing with SQLite).

Justfile recipes ​

RecipeCommand
just migrate msg=""uv run alembic revision --autogenerate -m "msg"
just upgradeuv run alembic upgrade head
just downgradeuv run alembic downgrade -1

Compatibility ​

Works with both blank and fastapi templates. When used with fastapi, the get_session dependency is wired for use with route handlers.

NOTE

The sqlalchemy addon provides the ORM layer but does not include a database driver or compose service. Add the postgres addon for asyncpg and a PostgreSQL container, or configure your own database URL in .env.

Released under the MIT License.