I have a table of users (User), and need to create a new table to track which users have referred other users. So, basically, I’m creating a many-to-many relation between rows in the same table.
So I’m trying to create table UserReferrals with the columns UserId and UserReferredId. I made both columns a compound primary key. And both columns are foreign keys that link to User.UserID.
Since deleting a user should also delete the relationship, I set both foreign keys to cascade deletes. When the user is deleted, any related rows in UserReferrals should also delete.
But this gives me the message:
'User' table saved successfully
'UserReferrals' table Unable to create relationship 'FK_UserReferrals_User'. Introducing FOREIGN KEY constraint 'FK_UserReferrals_User' on table 'UserReferrals' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint. See previous errors.
I don’t get this error. A cascading delete only deletes the row with the foreign key, right? So how can it cause “cycling cascade paths”?
Thanks for any tips.
After thinking about this, I’m starting to think the problem is not so much related to cyclic cascade paths as much as it may be related to multiple cascade paths.
Although the two UserIDs in my joining table will always be different, nothing prevents them from being the same. If they both referred to the same user, and that user was deleted, there would be multiple cascade paths to the joining table.