SQL Server Two foreign keys referencing same primary key cause cycles or multiple cascade paths, but why?

7fhtutme  于 2023-10-15  发布在  其他
关注(0)|答案(1)|浏览(87)

I am trying to define some constraints on some tables in my database:

Destinations : ID (PK), Description
Pallets : ID (PK), ID_Destination1 , ID_Destination2

I would like to define ID_Destination1 and ID_Destination2 as foreign keys referencing ID of Destinations .

However, when I try to define these foreign keys with constraints ON UPDATE CASCADE and ON DELETE SET NULL , I get the following error:
Introducing FOREIGN KEY constraint 'FK_PalletDest1' on table 'Pallets' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

I managed to create the table defining only one foreign key with constraint ON UPDATE SET NULLON DELETE SET NULL , but I couldn't create both foreign keys.

I read that one solution could be to set ON UPDATE NO ACTION and ON DELETE NO ACTION and to manage update/delete of Destinations within the code.

Why do these relations maybe cause cycles?

How do I bypass this error?

bbmckpt7

bbmckpt71#

Cascading deletes that would follow "multiple cascade paths" are simply not supported. It could be done, and there have been requests to implement it for years, but it's never been prioritized.

You can see and vote for feedback items here: https://feedback.azure.com/d365community/search/04fe6ee0-3b25-ec11-b6e6-000d3a4f0da0?q=multiple+cascade+paths

相关问题