Home > Enterprise >  Core data delete rule - Does it apply to the entity or the related entity?
Core data delete rule - Does it apply to the entity or the related entity?

Time:08-29

One JournalEntry has multiple Transactions.

enter image description here

In the first image, which is correct?

  1. When JournalEntry is deleted, Transactions are all deleted

  2. When any of the transactions is deleted, delete the JournalEntry

enter image description here

For the second picture, which is correct?

  1. When JournalEntry is deleted, delete all Transactions
  2. When Transaction is deleted, delete its JournalEntry

I understand the difference between nullify, cascade, do nothing and deny, but I find it really difficult to understand the direction...

In other words, when I define a delete rule on an entity's relationship, does the delete rule applies when the entity is deleted? or does it apply when the related entity is deleted?

CodePudding user response:

First Image: Neither When JournalEntry is deleted, delete all Transactions, and follow the relationships from those transactions and delete them as well, etc.

Second Image: Neither. When ANY transaction related to the JournalEntry is deleted, delete the JournalEntry, and all objects that JournalEntry has a relationship with, and so on.

Be Very, VERY careful with cascade. It can have VERY bad unintended consequences. In your second example, you would delete the JournalEntry, the Journal(I presume you are making an accounting program), the Company, all the other journals, etc.

When you are working with relationships, think about the Source and destination of the relationship. The source is the object you have at the moment, and the destination are all of the objects that you can reach from that object through som relationship.

You have 4 choices for your deletions: Nullify, Cascade, Deny and No Action. I will quickly dispose of No Action; don't use it. If you choose no action, any objects that currently have a relationship won't be told the object no longer exists, and this can leave the store in an I inconsistent state. If you tried to follow the relationship back to the deleted object, you will hit a fatal error.

Nullify is the default. It simply ends the relationship between the source and destination, and vice versa, when the source is deleted. You can still access and use the destination objects.

Deny prevents you from deleting an object if any other object still has a relationship to the object. This is the safest option in that you have to deal with each relationship before you can delete the source.

Cascade is the risky one. It will delete every destination object, and follow any relationships that the destination objects(now as the source object) has as a destination, and so on until there are no more destination objects. You could conceivably delete every object you have if you are not careful.

Choose your approach carefully. If you aren't choosing Nullify or Deny, ask yourself why.

  • Related