Guide 6 · Relational and Document Databases

SQLAlchemy, Deep Dive

The mapper and the query engine together: what a session really does, and the SQL your Python turns into.

Complete 20 notebooks 20 written

Before this guide: Object-Oriented Python, and Files, Paths and Formats.

The notebooks

Open in Colab runs a notebook in your browser, and Read shows it on GitHub without running it. Every notebook runs on its own, and the answers to its exercises are in a separate notebook, linked as Solutions.

1Why SQLAlchemyThe query you build in three ifs, and the string you stop concatenatingOpen in ColabReadSolutions
2Engines and URLscreate_engine, echo, the pool you did not ask for, and the in-memory database that looks empty from another threadOpen in ColabReadSolutions
3Connections and Transactionsconnect against begin, savepoints, and the rows that were never actually savedOpen in ColabReadSolutions
4Reading ResultsRow, scalars, one, and the result you already consumedOpen in ColabReadSolutions
5Tables and MetadataTable, MetaData, constraints that have names, and reflecting a database you did not designOpen in ColabReadSolutions
6SQL Expressionsselect, insert, update, and delete as objects, and the where clause that quietly lost half of itselfOpen in ColabReadSolutions
7Declarative ModelsDeclarativeBase, Mapped and mapped_column, and the annotation it cannot mapOpen in ColabReadSolutions
8Column TypesNaive datetimes, the precision SQLite quietly loses, and the type that only accepts a datetimeOpen in ColabReadSolutions
9The Sessionsessionmaker, add, flush, commit, and what autoflush does behind your backOpen in ColabReadSolutions
10The Identity MapSession.get, two queries and one object, and the attribute that expired the moment you committedOpen in ColabReadSolutions
11RelationshipsForeignKey, back_populates, and the join condition it cannot determineOpen in ColabReadSolutions
12Many to Manysecondary, the association table, and the extra column it cannot reachOpen in ColabReadSolutions
13Loading StrategiesThe N plus 1 queries you did not know you wrote, selectinload against joinedload, and uniqueOpen in ColabReadSolutions
14Joins and Aggregatesjoin, aliased, group_by, any and has, and the warning that you built a cartesian productOpen in ColabReadSolutions
15Cascades and Deletesdelete-orphan against passive_deletes, and the SELECT that loads every child just to delete itOpen in ColabReadSolutions
16Async SQLAlchemyAsyncSession and async_sessionmaker over a database that gains nothing from them, and the greenlet error from one unloaded attributeOpen in ColabReadSolutions
17Migrations with Alembicautogenerate, upgrade, downgrade, and the batch mode SQLite makes you useOpen in ColabReadSolutions
18Four Databases, One CodebasePostgreSQL, MySQL, SQL Server, and Oracle from one set of models, and the statements that still belong to one databaseOpen in ColabReadSolutions
19Testing a Data LayerA fixture that builds the schema once, a transaction rolled back after every test, and the object that detached when the session closedOpen in ColabReadSolutions
20A Complete Data LayerSchema, migration, load, and query in one script against one small databaseOpen in ColabReadSolutions