Two FKs pointing to same parent column - ON UPDATE CASCADE - SQL Server

107 views Asked by At

Here's the scenario. - Parent Table: TEAMMEMBERS, with a primary key RecID - Child Table: TEAMMEMBERTASKS

I have two columns in the TEAMMEMBERTASKS table, ReportedBy and AssignedTo. Both of these columns use the RecID to store which team member reported a task and which team member the task is assigned to. The RecID could be the same for both columns, but that is not always the case.

I need to add in a FK for both child columns that check the relationship to the parent, and I would like to add ON UPDATE CASCADE to both of the foreign keys.

Whenever I try to do this, my second foreign key throws a 'may cause cycles or multiple cascade paths' error.

Here's my code:

ALTER TABLE [dbo].[TEAMMEMBERTASKS]  WITH CHECK ADD  CONSTRAINT 
[FK_AssignedTo_TeamMemberRecID] FOREIGN KEY([AssignedTo])
REFERENCES [dbo].[TEAMMEMBERS] ([RecID])
GO

ALTER TABLE [dbo].[TEAMMEMBERTASKS] CHECK CONSTRAINT 
[FK_AssignedTo_TeamMemberRecID]  
GO

ALTER TABLE [dbo].[TEAMMEMBERTASKS]  WITH CHECK ADD  CONSTRAINT 
[FK_ReportedBy_TeamMemberRecID] FOREIGN KEY([ReportedBy])
REFERENCES [dbo].[TEAMMEMBERS] ([RecID])
ON UPDATE CASCADE
GO

ALTER TABLE [dbo].[TEAMMEMBERTASKS] CHECK CONSTRAINT 
[FK_ReportedBy_TeamMemberRecID]
GO

With the current code, will this cause the RecID to be updated in both child columns or will it cause the update command to be restricted?

Should I just go ahead and write up a trigger that deals with this instead?

0

There are 0 answers