I have a simple model that contains a one-to-many relationship to itself to represent a simple tree structure. It is set to cascade deletes so deleting the parent node deletes the children.
Unfortunately I get an error when I try to batch delete. A test demonstrates:
@Model final class TreeNode {
var parent: TreeNode?
@Relationship(deleteRule: .cascade, inverse: \TreeNode.parent)
var children: [TreeNode] = []
init(parent: TreeNode? = nil) {
self.parent = parent
}
}
func testBatchDelete() throws {
let config = ModelConfiguration(isStoredInMemoryOnly: true)
let container = try ModelContainer(for: TreeNode.self, configurations: config)
let context = ModelContext(container)
context.autosaveEnabled = false
let root = TreeNode()
context.insert(root)
for _ in 0..<10 {
let child = TreeNode(parent: root)
context.insert(child)
}
try context.save()
// fails if first item doesn't have a nil parent, succeeds otherwise
// which row is first is random, so will succeed sometimes
try context.delete(model: TreeNode.self)
}
The error raised is:
CoreData: error: Unhandled opt lock error from executeBatchDeleteRequest Constraint trigger violation: Batch delete failed due to mandatory OTO nullify inverse on TreeNode/parent and userInfo {
NSExceptionOmitCallstacks = 1;
NSLocalizedFailureReason = "Constraint trigger violation: Batch delete failed due to mandatory OTO nullify inverse on TreeNode/parent";
"_NSCoreDataOptimisticLockingFailureConflictsKey" = ( );
}
Interestingly, if the first record when doing an unsorted query happens to be the parent node, it works correctly, so the above unit test will actually work sometimes.
Now, this can be "solved" by changing the reverse relationship to an optional like so:
@Relationship(deleteRule: .cascade, inverse: \TreeNode.parent)
var children: [TreeNode]?
The above delete will work fine. However, this causes issues with predicates that test counts in children, like for instance deleting only nodes where children is empty for example:
try context.delete(model: TreeNode.self,
where: #Predicate { $0.children?.isEmpty ?? true })
It ends up crashing and dumps a stacktrace to the console with:
An uncaught exception was raised
Keypath containing KVC aggregate where there shouldn't be one; failed to handle children.@count
(the stacktrace is quite long and deep in CoreData's NSSQLGenerator)
Does anyone know how to work around this?