Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

sql server - cascading deletes causing multiple cascade paths

I am using SQlServer 2008, and an extract of some datatables is displayed below:

Users

Id (PK)

UserItems

UserId (PK) ItemId (PK) - (Compound key of 2 columns) ...

UserItemVotes

UserId (PK) ItemId (PK) VoterId (PK) - (Compound key of 3 columns)

I have the following relationships defined:

  • User.Id -> UserItems.UserId
  • (UserItems.UserId, UserItems.ItemId) -> (UserItemVotes.UserId, UserItemVotes.ItemId)
  • UserId.Id -> UserItemVotes.VoterId

Now, I am having a problem when turning on cascading deletes. When adding the 3rd relationship I receive the error "...may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints." I do not really want to do this, ideally if a user is deleted I would like to remove their useritem and/or their votes.

Is this a bad design? Or is there a way to get behaviour I want from SQL Server?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The approved answer is not a good answer. The scenario described is not bad design, nor is it "risky" to rely on the database to do its job.

The original question describes a perfectly valid scenario, and the design is well thought-out. Clearly, deleting a user should delete both the user's items (and any votes on them), and delete the user's votes on any item (even items belonging to other users). It is reasonable to ask the database to perform this cascading delete when the user record is deleted.

The problem is that SQL Server can't handle it. Its implementation of cascading deletes is deficient.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...