CREATE TABLE post_tag (
post_id integer not null,
tag_id integer not null,
CONSTRAINT fk_post
FOREIGN KEY (post_id)
REFERENCES post(id)
ON DELETE CASCADE,
CONSTRAINT fk_tag
FOREIGN KEY (post_id)
REFERENCES tag(id)
ON DELETE CASCADE
)
Here's a simple example using a cascade delete, with also a trigger
https://gist.github.com/dharrigan/7d5c6474cd56f0a17922f106c9968036
btw, it's a bit dangerous, in the sense that if you delete a post, that references a tag, that then references another post, then that post will be removed too.
you probably either want to put a restrict in, or remove the cascade delete on the tag 🙂
(and let the trigger do the delete for you)
say this is my schema definition
(postgres)
if a post is deleted, will just the (post_id, tag_id)
be deleted
or will the referenced tag
be deleted as well
and if its just the join table's tuple
how could I make it truly "crawl" the foreign keys and delete everything?
You could write a trigger to do that