Guide 8 · Relational and Document Databases

Peewee, Deep Dive

A light object-relational mapper for scripts and small applications, from model to migration.

Complete 12 notebooks 12 written

Before this guide: Object-Oriented Python.

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 PeeweeA single peewee.py module with no required dependencies, and the cases where SQLAlchemy winsOpen in ColabReadSolutions
2Models and FieldsMeta.database, create_tables, and the model that was bound to no database at allOpen in ColabReadSolutions
3Creating and Changing Rowscreate, save, insert_many, and the save() that silently inserted nothingOpen in ColabReadSolutions
4Selecting Rowswhere, order_by, fn.COUNT, and the and that quietly dropped half your filterOpen in ColabReadSolutions
5Transactionsatomic, savepoints, and the exception you caught that committed anywayOpen in ColabReadSolutions
6RelationshipsForeignKeyField, backref, and the join that still ran one query per rowOpen in ColabReadSolutions
7prefetch and LoadLoad, per_parent, and the backref that no join can flattenOpen in ColabReadSolutions
8JSON ColumnsJSONField, path queries, and the numeric filter that compared JSON text until as_int()Open in ColabReadSolutions
9FTS5Model and SearchFieldSearchField, web_query, and the search box that is secretly a query languageOpen in ColabReadSolutions
10Migrationspwmigrate, the schema diff, and the down() nobody testedOpen in ColabReadSolutions
11SQLite and PostgreSQLOne models module, two backends, and the SQL that changes when you swap the URLOpen in ColabReadSolutions
12A Small CatalogModels, migrations, search, and what the backend switch costs the rankingOpen in ColabReadSolutions