Share via

help me with this error

medooouni1980 0 Reputation points
2024-12-26T12:03:52.3633333+00:00

This error appears when trying to cascade the relationship between a primary key and a foreign key

"

'Employees' table saved successfully

'PC_details' table

  • Unable to create relationship 'FK_PC_details_Employees'.

Introducing FOREIGN KEY constraint 'FK_PC_details_Employees' on table 'PC_details' 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 or index. See previous errors.

"

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

0 comments No comments

2 answers

Sort by: Most helpful
  1. LiHongMSFT-4306 31,621 Reputation points
    2024-12-27T04:00:01.3266667+00:00

    Hi @medooouni1980

    How about using ON DELETE SET NULL. The FK will be set to NULL rather than causing a cascading delete. Or you could use triggers to implement such logic.

    Refer to this article for more details: Fix Error 1785 “Introducing FOREIGN KEY constraint … may cause cycles or multiple cascade paths” in SQL Server.

    Best regards,

    Cosmog


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Was this answer helpful?

    0 comments No comments

  2. Erland Sommarskog 134.7K Reputation points MVP Volunteer Moderator
    2024-12-26T12:55:24.25+00:00

    Cascading foreign keys can certainly make things easier, but there are many restrictions with them. Typically, if you have two or more FK columns in a table that refers to the same parent table, you can only have one if them as cascading, and the remaining needs to be NO ACTION.

    I should point out that there is nothing wrong with having multiple FK columns referring to the same table - that's a quite normal thing to do.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.