Chapter 3 of the SQLAlchemy 2 in Practice book covers implementing one-to-many relationships using SQLAlchemy ORM. It walks through creating a separate Manufacturer model, defining foreign keys, and adding relationship attributes with back_populates. The chapter explains lazy vs. eager loading strategies (select, joined, selectin, subquery, immediate, write_only, etc.) and their performance implications, including the N+1 query problem. It also covers cascade configurations ('save-update, merge' vs. 'all, delete-orphan') for handling deletion and detachment of related objects, with practical code examples throughout.

34m read timeFrom blog.miguelgrinberg.com
Post cover image
Table of contents
When to Use a RelationshipOne-To-Many Relationship ImplementationA Revised Importer ApplicationOne-To-Many Relationship QueriesLazy vs. Eager RelationshipsDeletion of Related Objects with a CascadeExercises

Sort: