SQLAlchemy Cascading Deletes - DEV Community
I thought I’d do a quick write up of something that I’ve found confusing for a while now, but never taken the time to look into. Anyone who works with databases has probably heard of foreign key constraints — used wisely, they keep us from making a total hash of our database. Using the classic example of artists and songs, foreign key constraints can help us make sure our song table isn’t littered with mysterious orphans that have no associated artist. When deleting an artist we’d usually like to have all of that artist’s songs deleted from the song table simultaneously. This is where the ON DELETE CASCADE option of foreign key constraints comes into play. Given that option, most databases will take care of “cascading” the parent deletion down to the children. Here’s an example from PostgreSQL. How does this translate into SQLAlchemy land? This is where I had some faulty assumptions. My assumption was that adding the cascade argument to the relationship resulted in the same schema as a
I thought I’d do a quick write up of something that I’ve found confusing for a while now, but never taken the time to look into. Anyone who works with databases has probably heard of foreign key constraints — used wisely, they keep us from making a total hash of our database. Using the classic example of artists and songs, foreign key constraints can help us make sure our song table isn’t littered with mysterious orphans that have no associated artist. When deleting an artist we’d usually like to have all of that artist’s songs deleted from the song table simultaneously. This is where the ON D
Explore this link on the map →related reading
- Cascades - SQLAlchemy 2.0 Documentationdocs.sqlalchemy.org
- The only scalable delete in Postgres is DROP TABLE — PlanetScaleplanetscale.com
- Glossary - SQLAlchemy 1.4 Documentationdocs.sqlalchemy.org
- The Flask Mega-Tutorial, Part IV: Database - miguelgrinberg.comblog.miguelgrinberg.com
- SQLAlchemy - Quick Guidetutorialspoint.com
- Object Relational Tutorial - SQLAlchemy 1.3 Documentationdocs.sqlalchemy.org
- Here is the reason why SQLAlchemy is so popular. | Towards Data Sciencetowardsdatascience.com
- Working with Database Metadata - SQLAlchemy 2.0 Documentationdocs.sqlalchemy.org
- Avoiding the soft delete anti-pattern · Cultured Systemscultured.systems
- SQLAlchemy in Flask | G-Testerhuohuoren4.github.io
- SQL (Relational) Databases - FastAPIfastapi.tiangolo.com
- Links | Isar Databaseisar.dev