Post

Replies

Boosts

Views

Activity

Reply to SwiftData self-referencing model has wierd behavior
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 } }
Aug ’23