Batch delete failed due to mandatory OTO nullify inverse

I get this error with NSBatchDeleteRequest in the model I have...


Constraint violation: Batch delete failed due to mandatory OTO nullify inverse on Leg/parentNode


The model is like this:

- Entity Node, has non-optional to-many relationship to entity Leg, with cascade delete rule

- Entity Leg has non-optional to-one relationship to entity Node, with nullify delete rule


What is the reason for the error and how should I alter the model to prevent this?

Accepted Reply

It seems to me that the problem is that this:

Entity Leg has non-optional to-one relationship to entity Node, with nullify delete rule

creates a situation where the delete rule invalidates your data model. In other words, specifying 'non-optional' means nullify isn't valid. That doesn't normally cause problems because in the same transaction that you invoked the delete rule you'd be assigning a different value for the relationship.

But your batch delete doesn't have that option, so the batch delete has to fail.


From your description, it sounds like you expected cascade-delete on the the Legs side of the relationship, but you specified nullify.

Replies

This error is worked around but making the Node.legs relationship be Optional.


I would like to understand better why is that needed. I expected that deleting all the legs will simply yield empty Set, but apparently it seems no Set exists at all.

It seems to me that the problem is that this:

Entity Leg has non-optional to-one relationship to entity Node, with nullify delete rule

creates a situation where the delete rule invalidates your data model. In other words, specifying 'non-optional' means nullify isn't valid. That doesn't normally cause problems because in the same transaction that you invoked the delete rule you'd be assigning a different value for the relationship.

But your batch delete doesn't have that option, so the batch delete has to fail.


From your description, it sounds like you expected cascade-delete on the the Legs side of the relationship, but you specified nullify.

I agree this seems to be the cause, although I'm a bit lost how to alter this.


I do want to Leg to Node to be nullify. Meaning - when a Leg is deleted, I want the other end of the relationship to be updated, so Node.legs has one item less. My understanding is that nullify is used for that.

Let me double check my assumptions:

- You're performing the batch delete on Leg entity instances

- The mandatory to-many relationship specifies a minimum size greater than 0


Is this right?

No, there is nothing mandated, no minimum and no maximum.


Node.legs is to-many non-optional relationship. The way I understand it, that means relationship exists always, but it can have 0 elements in the Set. Main reason for non-optional is to avoid unnecessary optional in the model class.


Your question got me thinking - what if I specify that 0 is minimum, maybe it would work. Although I find it hard that anything else than 0 is fitting to use as default. The model I work with has advanced quite a bit in the meantime, but I'll try to replicate this.