How do I query in Fluent (in Vapor) for items with a nil value for an OptionalParent relation?

1 day ago 1
ARTICLE AD BOX

Given Fluent's example of an OptionalParent relationship:

final class Planet: Model { // Example of an optional parent relation. @OptionalParent(key: "star_id") var star: Star? }

How do I query for planets with no star parent? I tried this but it doesn't work:

let wanderers = try await Planet.query(on: myDb) .filter(\.$star.id == nil) //error: Cannot infer key path type from context; consider explicitly specifying a root type

I also tried .filter(\.$star == nil) and got a longer error about not being able to apply == to these operands.

Read Entire Article