How to set up ON DELETE CASCADE in Supabase Postgres

Supabase delete cascade

Have a Supabase table setup with foreign keys and want to get rid of the rows in one table when their references in another table are removed?

The SQL CASCADE constraint is designed to do exactly this.

Quoting the PostgreSQL documentation:

CASCADE specifies that when a referenced row is deleted, row(s) referencing it should be automatically deleted as well.

To make it work in Supabase's Postgres database, we have to adjust our tables.

Enter your table and column names below and the code will be automatically generated for you.

Once you're done, copy the generated code and run it in the SQL editor of your Supabase admin panel.

ALTER TABLE public.children
DROP CONSTRAINT children_parent_id_fkey,
ADD CONSTRAINT children_parent_id_fkey
FOREIGN KEY (parent_id)
REFERENCES parents(id)
ON DELETE CASCADE;

Reference

Check out the answer by Justus Bl├╝mer on Stack Overflow to learn more.


background scene
Join the garden
Finally enjoy quality-first content.
'cause low-quality sucks

legal privacy