Python reference

SQLAlchemy
cheat sheet.

SQLAlchemy ORM and Core queries, relationships, transactions, async sessions, and FastAPI integration, with Python and SQL examples.

27 topics114 code examplesUpdated
How SQLAlchemy fits together
01Model describesClasses and tables define data.
02Statement buildsPython expressions become SQL.
03Session runsOne transaction tracks changes.
04Database commitsRows are stored or rolled back.
Jump to a specific topic

Choose a chapter

4 chapters · 27 topics

CHAPTER 01 / 04

Models, sessions, and queries

Define SQLAlchemy models and manage sessions, CRUD, and SELECT queries. Compare Python with SQL and load relationships without N+1 queries.

9 topics
CHAPTER 02 / 04

Advanced queries and async sessions

Write SQLAlchemy bulk updates, subqueries, CTEs, and window functions. Use AsyncSession safely and connect database sessions to FastAPI.

8 topics
CHAPTER 03 / 04

Mappings, connections, and dialects

Extend SQLAlchemy mappings and inheritance, run Core transactions, and configure dialects, isolation, and events with Python and SQL examples.

6 topics