A Pydantic model and a database table in one class, and the places that convenience stops.
Before this guide: APIs and JSON, and SQLAlchemy, Deep Dive.
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.
| 1 | table=TrueThe schema and the model in one class, and the table that is already defined | Open in ColabReadSolutions |
| 2 | Engine and create_allSQLModel.metadata.create_all, no such table, and the foreign keys SQLite ignores | Open in ColabReadSolutions |
| 3 | Sessionssqlmodel.Session, expire_on_commit, and the id that was still None | Open in ColabReadSolutions |
| 4 | Field Types and Defaultsdatetime, Enum, UUID, and the dict that has no matching SQLAlchemy type | Open in ColabReadSolutions |
| 5 | Reading Rowssession.exec, one() when two rows match, and the Row tuple you did not expect | Open in ColabReadSolutions |
| 6 | Validation and table=TrueThe validator that silently never ran on your table model | Open in ColabReadSolutions |
| 7 | Create, Read and Update Modelsmodel_validate, sqlmodel_update, and the update that wrote None over every field | Open in ColabReadSolutions |
| 8 | Relationshipsback_populates, cascade_delete, and the heroes orphaned by a deleted team | Open in ColabReadSolutions |
| 9 | Many to ManyLink models, a composite primary key, and the extra column that landed NULL | Open in ColabReadSolutions |
| 10 | Loading and N+1sa_relationship_kwargs, and the 200 queries hiding behind one loop | Open in ColabReadSolutions |
| 11 | sa_column and __table_args__Real column types, unique constraints, and where SQLModel hands you back to SQLAlchemy | Open in ColabReadSolutions |
| 12 | MigrationsThe import Alembic leaves out of the script it generates, and the column create_all will never add | Open in ColabReadSolutions |
| 13 | SQLModel in FastAPIA session dependency, response_model, and the response that referred to itself | Open in ColabReadSolutions |
| 14 | A Small ServiceTable models, public models, a migration, and a tested API in one notebook | Open in ColabReadSolutions |