liveBook · Manning

On delete cascade

As explained in Designing AI Systems

ON DELETE CASCADE is a relational database constraint action that automatically deletes dependent rows when their referenced row is deleted.

ON DELETE CASCADE is a foreign-key action that automatically deletes dependent rows when the referenced row is deleted. In session storage, deleting a session removes every message belonging to it, preventing orphaned records and avoiding a manual delete sequence. section 4.4.1 section 4.4.2

Use the constraint to make parent-child deletion behavior part of the database schema:

  • Let delete_session remove the session and rely on PostgreSQL to remove its messages automatically. section 4.4.2

Deleting a session

A session has many rows in messages through session_id. When delete_session deletes that session, PostgreSQL deletes the associated messages in the same database relationship, rather than rejecting the deletion or leaving orphaned rows. section 4.4.1 section 4.4.2

How Designing AI Systems covers it

How deep it goes
Defines the foreign-key action and its operational effect, but leaves the delete_session implementation as an exercise.
Examples
Uses sessions and messages linked by session_id to demonstrate automatic dependent-row deletion.
What it assumes
Assumes familiarity with tables and foreign keys while illustrating parent-child deletion through sessions and messages.
What you can do after
Operate/configure PostgreSQL schemas so deleting a session automatically removes its associated messages.
How it connects
Connects foreign keys, referential integrity, relational databases, database schemas, and session storage.

Also explained in

Compare how 3 books explain on delete cascade →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Designing AI Systems. The section links point to the parts of the book it draws on.