Reputation: 8130
I have two entities. One is driver and one is cars
a driver can have many cars, a car can only have one driver.
This is an inverse relationship, and optional on both sides.
When I delete a driver, the car is "orphaned" It still exists, but there is no longer any related driver.
I would like the car to be destroyed when its driver no longer exists. This may not make logical sense, but this is a learning exercise.
I uncheck the optional checkbox for the relationship within the cars entity. I would figure this means a car must have a driver in order to exist.
When I delete my driver object, it is unable to save the context. I receive this error.
"The operation couldn’t be completed. (Cocoa error 1570.)" UserInfo=0xc586430 {NSValidationErrorObject= (entity: Cars; id: 0xc580d30 ; data: { driver = nil; make = A; model = B; year = 222; })
Why does this work when both relationships are optional.. but once I change the relationship, it no longer works?
Upvotes: 0
Views: 388
Reputation: 8130
Okay.. it did not have to do with optional or not optional. I make it so that a car has to have a driver. then i delete the driver, but that breaks the rules since the car is left over with no driver.
the delete rule has to be set to cascade so that it deletes the related car.
Upvotes: 0
Reputation: 3084
Why not set your driver -> car relationship to Cascade on delete? The reason that your operation fails is the same as what happens when you try to create a driver object without a car (if the relationship is mandatory), validation fails.
Upvotes: 1