A practical chapter from Miguel Grinberg's SQLAlchemy 2 book covering many-to-many relationships. Explains how to implement them using a join table (via SQLAlchemy's Table class and the secondary argument in relationship()), with a products-countries example. Covers model definitions, querying with chained joins, deletions, and detaching linked entities. Also introduces Alembic for database migrations, including setup, auto-generating migration scripts, and running upgrades.

24m read timeFrom blog.miguelgrinberg.com
Post cover image
Table of contents
What Is a Many-To-Many Relationship?How Many-To-Many Relationships WorkA Simple Many-To-Many Relationship ImplementationProduct Importer Script UpdatesMany-To-Many Relationship QueriesDeleting From Many-To-Many RelationshipsDatabase MigrationsExercises

Sort: