Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths. Specify on delete no action

前端 未结 3 837
情书的邮戳
情书的邮戳 2021-01-01 18:01

User, Employer, Candidates and Job, an employer can create multiple jobs and each job can only have one employer, a candidate can apply for many jobs and each job can have m

3条回答
  •  误落风尘
    2021-01-01 18:40

    I realize this is rather old but I ran into this same issue and there are some comments that didn't get addressed.

    Specifically "you've hidden the problem..."

    Cascade from multiple paths should work because it's valid that deleting either ancestor should have the effect of deleting the descendant. But that's only in theory, in reality SQL Server just doesn't allow it, hence the error. One solution is in this post here solving-the-sql-server-multiple-cascade-path-issue-with-a-trigger.

    He suggests removal of all the offending cascade actions and replacing them all with triggers to delete the records. I prefer to work at the top level of the cascade chain. In his example I'd just cut it off at the 'parent' record with INSTEAD OF DELETE for the children and let cascade take care of the rest.

    I do this for two reasons

    1. It should be done in the database because it's the last line of defense for bad data... kind of like emptying the pockets just before the clothes go in the washer. Taking care of things there means you don't have to replicate the code into all the different models you might build off this one DB in the future, nor do you have to count on all the newbie devs to take care of it.

    2. doing it at the topmost ancestor will allow all the other relationships to remain including ones you might add in the future.

    Hope this helps, Mike

提交回复
热议问题