Understanding Core Data delete rules on One to Many
Asked Answered
G

3

65

I am a little fuzzy about Core Data Relationships deletion rules. So if someone could help me answer a few questions about them.

I have Entities A and B. A has a to-Many relationship with B, and B has a to-One relationship with A.

A<--->>B

Now, if I set the delete rule at A to Cascade, I understand it will delete all the Bs related to it. But if I set it to Nullify, will it set the Bs to NIL or just the Foreign Key to Nil?

And I looked everywhere about the relationship from B to A, should I set it to Nullify? Will that just Nullate the "B Object" at A? Or will it Nullify all the Bs associated with A? What about Cascade? Will it delete all the Bs associated with A, or just the particular B?

Or do I just use "No Action" on the relation from B to A so that when I delete B, no change will happen to A, but the reference to B won't exist?

I am PRETTY confused with these, so excuse my questions.

Thanks.

Georgiageorgian answered 25/3, 2015 at 12:13 Comment(1)
if i set it to "Nullify" on the One relationship (like suggested in the answers bellow) then NSBatchDeleteRequest(objectIDs: ...) raises an error. but setting it to "No Action", which works, gives compiler warnings. Apple's documentation is unsatisfactory. it's 2020 and there still isn't an answer to this?Anaanabaena
E
75

If you set the delete rule to "nullify" and delete the A object, then the references to that object in the Bs will be removed. The inverse works the same way. If you have cascade and delete B then it will remove the A that B pointed to. It will then follow the delete rule from A to the remaining Bs (either cascade or nullify).

The rules you set really depend on your data model. If A were a customer and B were their orders then you could set the A->B rule to deny (prevent A from being deleted if it the customer has orders) or cascade (delete the orders when the customer is deleted). The B->A rule would probably be "nullify". If an order is deleted simply remove the reference to the order from the customer.

The relationship delete rules are described in the Apple Core Data Programming Guide

Extranuclear answered 25/3, 2015 at 12:20 Comment(8)
And "Do nothing" will simple keep the reference, without it really existing, right?Georgiageorgian
Yes, Deleting A with a "No Action" may result in B's referring to a non-existent AExtranuclear
"Nullify Remove the relationship between the objects but do not delete either object. This only makes sense if the department relationship for an employee is optional, or if you ensure that you set a new department for each of the employees before the next save operation. It says nullify "do not delete either object"." This is really confusing. So if I really want to delete A, I can't set the rule to nullify?Abdel
Where is that quote from? It is incorrect. The object you delete is deleted, the references in other objects to the deleted object will be nullifiedExtranuclear
@Extranuclear That quote comes directly from the Apple Core Data Programming Guide under Relationship Delete Rules > Nullify. I'm not sure if it is incorrect because I'm getting duplicate destination objects after deleting the source object where the delete rule is nullify.Scrub
You wouldn't expect the destination object to be deleted if the rule is nullify. If you want the destination to be deleted when the source is deleted then the delete rule should be cascade. If what apple's document says is correct then you would need to delete an object twice if the rule is nullify. Once to clear the relationship and a second time to actually delete it. That doesn't make senseExtranuclear
Great example in the docs for the cascade rule "if you delete a department, fire all the employees in that department at the same time.". Sorry everyone, it's just that's how we had the database set up, you see.Daley
Can you explain about No Action. I didn't get clarity on that.Pelotas
F
19

More deep explanation and visualization with delete rule.


Let be assume we have database with table person and work. Single person can have many tasks to do.

Sample data and ER relationship

enter image description here enter image description hereenter image description here


Delete Rule Explation

  1. No Action : If I add this delete rule on relationship and then delete one of the person then it will not do anything with the task but person got deleted. The task still points to the person that we deleted.
  • Use Case: I don't think it is used anywhere.
  1. Nullify : If I apply this delete rule and delete the person then associate tasks will points to the null person. For example, I deleted the thor and thor tasks will point to the null person. Check below output.

enter image description here

  • Use Case : Let's assume we have DB with Person and address. Now person sells his house to broker. In this case you can null the person of that address and reassign the owner to that house when new Person purchased that house.
  1. Cascade : In this rule, if I deletes the person then it will deletes all the tasks associated with that person. For example, I deletes the Spiderman, check below output.

enter image description here

  • Use Case: Let be assume we have a user and his friends list. If user account deleted then we also want to delete the friend list.
  1. Deny : Now we have person i.e. IronMan with 2 tasks. In this rule, if I try to delete the Iron Man then it does not allow me to do. It gives error "The operation couldn’t be completed. (Cocoa error 1600.)" on saving the context. Now to delete the Iron Man person, we need to delete all his associated tasks and then we could able to delete the Person.
  • Use Case: Lets assume, there is a user with bank account. We can not delete the user from the bank until his account is not closed.

Fracture answered 30/12, 2019 at 8:7 Comment(1)
It's too nice bro.. Today I learned a new thing because of you. Thank you.Pelotas
C
3

if you set "nullify" and delete A's object , A's object and the back relationship only will get deleted not B's object.

eg : Customer entity has nullify relationship to orders . and if you delete a customer from customer entity, it will just delete customer and its back relationship from order . it wont remove the orders the customer made

but if the relationship is "cascade" it will remove the customer and his orders .

if it is "deny" . if the customer has any order , it wont delete delete the customer even .

Coltish answered 21/9, 2018 at 10:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.