EDIT: This is a duplicate I didn’t find it at first.
I am building a web-site for user-groups event management.
Members : Name, Id
Events : DateTime, Topic, OrganizerId (from FK to Members table)
EventRegistrations : MemberId (FK), EventId (FK)
Description (redundant) :
A member can create and event, and becomes this event’s organizer.
Any member can register for an event, and a record in EventRegistrations is created.
The Problem:
When I create PK-FK dependency between the tables, I get an error saying:
Introducing FOREIGN KEY constraint ‘reg_evt_fk’ on table ‘eventregistrations’ may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Question:
What is a good way to deal with this? I have two solutions to begin with:
- Introduce 4th (associating) table “Organizers : MemberId -> EventId”.
- Disable CASCADE DELETE on one of the dependencies, and do check programmatically before DELETE operations.
I’m looking for suggestions or a feedback on the above. Extra explanations/comments are appreciated.
Note: DB is SQL Server 2008, but it shouldn’t matter, I think.
EDIT: Suppose deleting an event IS required behavior (example is simplified).
I guess this is not a circular dependency, but rather a “multiple cascade path” problem (don’t remember the exact error message).
The solution I’d prefer is to remove the OrganizerId CASCADE DELETE, since deleting an organizing member should not automatically remove the member’s events, and implement this part as an SP.
You can still implement child record deletion in an INSTEAD OF DELETE trigger, or in an SP.