Data integrity when deleting objects with ordered many-to-many relationships?

I have a problem deleting objects that have ordered, many-to-many relationships.


Simplified model:


Container.items (ordered, Nullify) <<-->> Item.containers (Nullify)

Container.image (Cascade) <-> Image.container


Consider a single container with two items, already saved. Delete the container. Search for all items that have no container, i.e.: "containers.@count == 0". Performing a fetch returns no objects.


In contrast, search for "containers.@count == 1". Returns both items. Then inspect those items, and they have no containers. Inspect the database, and the join table still has entries which refer to containers that do not exist.


When I change the relationship to make it unordered, the problem goes away.


I have seen various problems reported with ordered relationships on Stack Overflow. So this may be related. However, in a sample project, I am unable to reproduce the issue.


Is there some way I can (indirectly) force those join entries to go away? What could I be doing wrong that would cause Core Data to fail to clean up its join table correctly?


It's clear from logging the SQL that the DELETE commands for the join table are not being issued, so something is confusing Core Data such that it thinks that the Container objects are still around.

The only workaround I have found is to delete the Items that are about to be orphaned before deleting the Container. But this is not required in my test app, so there is definitely some other variable affecting the situation. I will use the workaround for now, but it seems ugly.

Data integrity when deleting objects with ordered many-to-many relationships?
 
 
Q