Hi,
I am currently experiencing some trouble when using parent model property in a predicate of a child model.
I have an Item class that define parent-child relationship:
@Model class Item {
var timestamp: Date
@Relationship(inverse: \Item.children)
var parent: Item?
var children: [Item]
init(parent: Item? = nil, children: [Item] = [], timestamp: Date = .now) {
self.parent = parent
self.children = children
self.timestamp = timestamp
}
}
I subclass this model like that:
@available(iOS 26, *)
@Model final class CollectionItem: Item { /* ... */ }
When i make a Query in my View like that the system crashes:
@Query(
filter: #Predicate<CollectionItem> { $0.parent == nil },
sort: \CollectionItem.name,
)
private var collections: [CollectionItem]
CrashReportError: Fatal Error in DataUtilities.swift
AppName crashed due to fatalError in DataUtilities.swift at line 85.
Couldn't find \CollectionItem.<computed 0x000000034005d4e8 (Optional<Item>)> on CollectionItem with fields [SwiftData.Schema.PropertyMetadata(name: "name", keypath: \CollectionItem.<computed 0x000000034003c120 (String)>, defaultValue: nil, metadata: nil), SwiftData.Schema.PropertyMetadata(name: "icon", keypath: \CollectionItem.<computed 0x000000034003ca04 (Optional<String>)>, defaultValue: nil, metadata: nil), SwiftData.Schema.PropertyMetadata(name: "timestamp", keypath: \Item.<computed 0x0000000340048018 (Date)>, defaultValue: nil, metadata: nil), SwiftData.Schema.PropertyMetadata(name: "parent", keypath: \Item.<computed 0x0000000340048a4c (Optional<Item>)>, defaultValue: nil, metadata: Optional(Relationship - name: , options: [], valueType: Any, destination: , inverseName: nil, inverseKeypath: Optional(\Item.<computed 0x0000000340048fe8 (Array<Item>)>))), SwiftData.Schema.PropertyMetadata(name: "children", keypath: \Item.<computed 0x0000000340048fe8 (Array<Item>)>, defaultValue: nil, metadata: nil)]
When I query as Item it works but then i cannot sort on CollectionItem field and must add unnecessary down casting:
@Query(
filter: #Predicate<Item> { $0.parent == nil && $0 is CollectionItem },
)
private var items: [Item]
Am I missing something? Is it a platform limitation or a known issue?
Selecting any option will automatically load the page