sql-serverclass-table-inheritance

How to solve multiple cascade paths?


In my database, I have a Person and Member table. The Member table has 2 foreign keys which both reference the same column in the People Table. The constraints look like this:

CONSTRAINT [FK_Members_People_1] 
  FOREIGN KEY ([PersonID])      
  REFERENCES [People].[People]([ID])
  ON DELETE CASCADE
  ON UPDATE NO ACTION,

CONSTRAINT [FK_Members_People_2] 
  FOREIGN KEY ([EnquiryTakenBy]) 
  REFERENCES [People].[People]([ID])
  ON DELETE SET NULL
  ON UPDATE NO ACTION

The error I get is as follows:

Introducing FOREIGN KEY constraint 'FK_Members_People_2' on table 'Members' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

How would I go about solving this?

Basically this is the behaviour I'm trying to get:

EDIT: Table structure is as follows:

CREATE TABLE [People].[People]
(
    [ID]                    INT          NOT NULL IDENTITY,
    [PersonType]            INT          NOT NULL,         
    [Forename]              VARCHAR(16)  NOT NULL,
    [Surname]               VARCHAR(32)  NOT NULL,
    [Gender]                CHAR(1)      NOT NULL,
    [DateOfBirth]           DATE         NULL,
    [HobbiesAndInterests]   VARCHAR(256) NULL,
    [AdditionalInformation] VARCHAR(512) NULL,
    [LocalCentre]           INT          NOT NULL  DEFAULT 0,

    CONSTRAINT [PK_People] PRIMARY KEY ([ID]),

    CONSTRAINT [FK_People_PersonType] 
      FOREIGN KEY ([PersonType])  
      REFERENCES [Lookups].[PersonTypes]([ID])
      ON DELETE CASCADE
      ON UPDATE CASCADE,

    CONSTRAINT [FK_People_Centres]    
      FOREIGN KEY ([LocalCentre]) 
      REFERENCES [Lookups].[Centres]([ID])
      ON DELETE CASCADE
      ON UPDATE CASCADE
)

CREATE TABLE [People].[Members]
(
    [PersonID]         INT          NOT NULL,
    [IsActive]         BIT          NOT NULL  DEFAULT 0,
    [Issues]           VARCHAR(500) NULL,
    [InTreatment]      BIT          NOT NULL  DEFAULT 0,
    [ProblemSubstance] VARCHAR(64)  NOT NULL,
    [WantsHelpWith]    VARCHAR(128) NULL,
    [EnquiryTakenBy]   INT          NOT NULL,
    [IsVolunteer]      BIT          NOT NULL  DEFAULT 0,

    CONSTRAINT [PK_Members] PRIMARY KEY ([PersonID]),

    CONSTRAINT [FK_Members_People_1] 
      FOREIGN KEY ([PersonID])      
      REFERENCES [People].[People]([ID])
      ON DELETE CASCADE
      ON UPDATE NO ACTION,

    CONSTRAINT [FK_Members_People_2] 
      FOREIGN KEY ([EnquiryTakenBy]) 
      REFERENCES [People].[People]([ID])
      ON DELETE SET NULL
      ON UPDATE NO ACTION

)

Solution

  • Ok, found a solution. Apparently my problem was something to do with SQL Server didn't know which constraint took precedence, so I replaced the second foreign key constraint with this trigger:

    CREATE TRIGGER Member_UpdateEnquiryID ON People.People
    AFTER DELETE
    AS
    BEGIN
       DECLARE @id int = (select id from deleted);
       UPDATE People.Members
       SET EnquiryTakenBy = NULL
       where EnquiryTakenBy = @id
    END