I have been trying to do something similar. I think you may need an inverse to the children collection. This is what I have done and it seems to work.
If someone else knows better I'd be delighted to be schooled otherwise :-)
import Foundation
import SwiftData
@Model
final class TreeNodeModel{
var createdAt: Date
var title: String
var content: String
// Parental relationship
public var parent: TreeNodeModel?
// Inverse
@Relationship(deleteRule:.cascade, inverse: \TreeNodeModel.parent) var children: [TreeNodeModel]?
init(title: String, content: String = "") {
self.title = title
self.content = content
}
}
Post
Replies
Boosts
Views
Activity
I have the same problem, SwiftData predicates do not appear to allow nil tests on references to other SwiftData classes - it will work for a String? for example. Trying to work out a workaround has driven me crazy (and I failed.)