On Aug 20, 2008, at 20:21, Nick Zitzmann wrote:

I have two Core Data entities. Entity X has a to-many relationship with entity Y. On entity X, the relationship may refer to zero or many instances of entity Y, and deleting an instance of entity X is supposed to cascade-delete instances of entity Y. On entity Y, the inverse-relationship back to X is mandatory, and deleting an instance of entity Y is supposed to nullify the relationship to entity X.

But when I try to delete a saved record belonging to entity Y, the - validateForDelete: method always says NO, and returns error 1600, "Items cannot be deleted from (name of inverse-relationship pointing to X)." Why doesn't this work? It should be delete-able because Core Data is supposed to nullify the relationship to the entity X record, which is not being deleted, and furthermore instances of entity X do not need to have any relationships with instances of entity Y.

Are you calling validateForDelete manually? If so, are you calling it before or after deleting the Y object?

The error seems technically correct, given how literal Core Data is. Y's Nullify setting for its to-X relationship means that Y, when deleted, is going to get removed from X's to-Y relationship, and that means that Y's to-X relationship will incidentally get set to nil. But you've specified that Y's to-X relationship is non-optional, so nil is not valid.

I think you have several choices:

-- Don't validate the deletion. The deleted Y object will be invalid, but I doubt save:error: will complain.

-- Make Y's to-X relationship optional. After all, being non-optional doesn't mean that it can't be nil, just that the object is invalid if it is nil. You can add custom validation for Y's to-X property, or for validateForInsert and validateForUpdate, if you really need to check.

-- Set Y's to-X relationship to No Action instead of Nullify.

I suspect that the last option might be your easiest way out.


_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to