SQLAlchemy Cheatsheet 03 — Session and Unit of Work
Cheatsheet: AsyncSession / Session, add/flush/commit/rollback, expire_on_commit, identity map, merge, scoped patterns.
Cheatsheet: AsyncSession / Session, add/flush/commit/rollback, expire_on_commit, identity map, merge, scoped patterns.
Manual migrations: op API, ALTER patterns, batch ops for SQLite, data migrations, idempotent migrations.
All Pydantic v2 validation hooks: field_validator, model_validator, mode=before/after, custom validators via Annotated.
Schema in SQLAlchemy 2.0: typed mapped columns, Python types vs DB types, defaults, indexes, constraints, table_args.
Cheatsheet: function/class decorators, @wraps, async decorators, contextmanager, asynccontextmanager.
Cheatsheet: model change → autogenerate → review → upgrade → ship.
Cheatsheet: op.create_table / drop / add_column / alter_column / indexes / constraints / FKs / raw SQL.
Cheatsheet: model_dump, exclude/include/exclude_none/exclude_unset, field_serializer, model_serializer.
Cheatsheet: select() / where / order_by / limit / offset / joins / aggregates / subqueries / CTEs / window funcs.
Alembic branching: how multiple revisions create branches, merging strategies, and team workflows.