Notes on on delete clauses

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the databases category.

Last Updated: 2024-04-26

Foreign keys sometimes contain ON DELETE clauses, as do the first two fields below:

CREATE TABLE order_items (
    product_no integer REFERENCES products ON DELETE RESTRICT,
    order_id integer REFERENCES orders ON DELETE CASCADE,
    quantity integer,
    PRIMARY KEY (product_no, order_id)
);

What are the possible settings?

The default is "NO ACTION"

Cascade and Web Frameworks (e.g. Rails)

Watch out - it skips lifecyle callbacks that might occur in your web framework.

Resources