I'm trying to remove all data (for debugging purposes). Let's say I have two models:
@Model
class School {
@Relationship(deleteRule: .nullify, inverse: \Student.school) var students: [Student]
init(students: [Student]) {
self.students = students
}
}
and
@Model
class Student {
var school: School?
init() {}
}
I'm trying to delete everything like this:
try modelContext.delete(model: School.self)
try modelContext.delete(model: Student.self)
But I'm getting the error Batch delete failed due to mandatory OTO nullify inverse on Student/school
. Why does it say "mandatory" when school
is an optional value in Student
?
To be able to use delete(model:)
for a type with a relationship then both sides of the relationship needs to be optional.
So you need to change School
@Relationship(deleteRule: .nullify, inverse: \Student.school)
var students: [Student]?
And if you don’t want to change your model then delete the objects one by one.
I assume this is a bug that nullify
is ignored and that it will be fixed in the future.