SwiftData `unsupportedPredicate` when `Query` via a relationship

private struct TransactionItemsView: View {
    init(journal: Journal, budgetMode: Bool) {
        let journalID = journal.id!
        _entries = .init(filter: #Predicate {
            $0.journal!.id! == journalID
        }, sort: \.date, order: .reverse) 

// HERE: Query encountered an error: SwiftData.SwiftDataError(_error: SwiftData.SwiftDataError._Error.unsupportedPredicate)

    }

    @Query var entries: [Entry]
...
}

My model:

// unimportant code like `init` is hidden
@Model
final class Journal {
    ...

    var id: UUID!

    @Relationship(deleteRule: .cascade, inverse: \Entry.journal) var entries: [Entry]! = []
}

@Model
final class Entry {
    ...

    @Relationship(deleteRule: .nullify) var journal: Journal!
}

I ran into the very same issue today. Turns out SwiftData does not support force-unwrap in Predicate. So you can try using if-let or .flatMap.

Using if-let worked for me, so something like this may work for you:

private struct TransactionItemsView: View {
    init(journal: Journal, budgetMode: Bool) {
        let journalID = journal.id!
        _entries = .init(filter: #Predicate {
            if let journal = $0.journal, let id = journal.id {
                 id == journalID
            } else {
                false
            }
        }, sort: \.date, order: .reverse) 

    }

    @Query var entries: [Entry]
...
}

I did submit feedback on this: FB13202879

SwiftData `unsupportedPredicate` when `Query` via a relationship
 
 
Q