Hey guys, i am trying to delete things that are re...
# help
k
Hey guys, i am trying to delete things that are related to some reference. For example I have post and that post have comments, if I as an owner delete that post from my profile, I want to delete all comments which are related to that post id. Is there any solution for it? I researched and found something like cascade deleting or something like that. I am not the expert in sql. 🙂
j
yup you're right, you'll need to use ON DELETE CASCADE when setting up a foreign key in the comments table: https://www.postgresql.org/docs/current/ddl-constraints.html
so when creating your comments table, it'll be something along the lines of
Copy code
CREATE TABLE comments ...
...
post_id uuid REFERENCES posts ON DELETE CASCADE,
...