Scenario
Imagine a chat server with conversations C and multiple posts P in each conversation. A user can reply to a post. This data should be stored as reply := (original_post_id, replyer_post_id).
To mimic this I have the tables P (posts) and R (replies):
CREATE TABLE P (
id BIGINT NOT NULL,
-- more data ...
)
CREATE TABLE R (
orig_id BIGINT NOT NULL,
repl_id BIGINT NOT NULL,
FOREIGN KEY orig_id REFERENCES P(id),
FOREIGN KEY repl_id REFERENCES P(id),
)
Problem
If I want to automaically delete all associated R-entries upon deletion inside P, I would change the last two lines of R's definition to:
...
FOREIGN KEY orig_id REFERENCES P(id) ON DELETE CASCADE,
FOREIGN KEY repl_id REFERENCES P(id) ON DELETE CASCADE,
)
However, I recieve the following error:
Introducing
FOREIGN KEYconstraint [...] on table [...] may cause cycles or multiple cascade paths. SpecifyON DELETE NO ACTIONorON UPDATE NO ACTION, or modify otherFOREIGN KEYconstraints.
Question
Why exactly am I recieving this error? Technically it is not a cycle as neither R.orig_id nor R.repl_id are referenced by any other table (thus possbibly creating a cycle).
I furthermore cannot see why multiple cascade paths should be an issue, as the deletion procedure would be as follows:
- Delete an entry from
PwithP.id = 1234 - Delete any entry from
RwithR.repl_id = 1234orR.orig_id = 1234
Should I somehow constrain the columns R.repl_id and R.orig_id not to be equal?