Hey everybody! Quick question. I'm trying to delet...
# help
f
Hey everybody! Quick question. I'm trying to delete an entry from my
categories
table, however it isn't deleting due to a foreign key constraint. I have a join table named
category_items
, which is where it's failing. How can I alter the table to allow for a
ON DELETE CASCADE
? I'm looking to automatically delete entries from
category_items
whenever attempting to delete an entry from
categories
.
Hmm, seems like this isn't a feature yet available in Supabase, according to this Github issue (https://github.com/supabase/supabase/issues/2518). That is, at least not in the web view of Supabase. There's gotta be a way to write raw SQL to handle this though!
f
That was actually one of the first things I came across when trying to fix it! I'm not great at raw SQL, but here's what I came up with:
Copy code
ALTER TABLE category_items
DROP CONSTRAINT category_items_category_id_fkey,
ADD CONSTRAINT category_items_category_id_fkey
   FOREIGN KEY (id)
   REFERENCES categories(id)
   ON UPDATE CASCADE
   ON DELETE CASCADE;
however Supabase returns the error:
Copy code
insert or update on table "category_items" violates foreign key constraint "category_items_category_id_fkey"