[Solved]-Where is the ON DELETE CASCADE logic being implemented in Django? (PostgreSQL is used)


If you are asking where the relevant code is implemented: You can find it here.

Implementing the CASCADE-DELETE logic in the application/ORM layer makes sense as this enables the app to get informed when deletion happens (eg. Django’s delete signals are fired for the deleted instances), furthermore it is a sane way to enable this feature across different types of databases.

If you are worried about the integrity of your data: Django still sets foreign key constraints if your database supports this (eg. check Postgresql). So your database won’t let you delete any rows a foreign key is pointing to.

Try it yourself:

> DELETE FROM accounts_publisher WHERE id=5;
ERROR:  update or delete on table "accounts_publisher" violates foreign key constraint "accounts_publisher_id_411559b18a178e73_fk_accounts_publisher_id" on table "accounts_membership"
DETAIL:  Key (id)=(5) is still referenced from table "accounts_membership".


FYI, this has been configurable since django 1.3:

Basically you need to set on_delete to DO_NOTHING and add the cascade logic to the DB yourself:

DO_NOTHING: Take no action. If your database backend enforces referential integrity, this will cause an IntegrityError unless you manually add a SQL ON DELETE constraint to the database field (perhaps using initial sql).

Leave a comment