Skip to content

Using Querybuilder with .field() or .fields() on models with optional relations crashes in SiblingsEagerLoader() #584

@rchav

Description

@rchav

Describe the bug

See discussion in Discord for full context: https://discord.com/channels/431917998102675485/1146265856834281573/1146265856834281573

Seeing a crash when using .fields() or .field() on a QueryBuilder object that has optional relations (Siblings + OptionalParents in this case)

let queryBuilder = Planet.query(on: req.db)
    .with(\.$spacePrograms)
        .join(siblings: \.$spacePrograms) // 1.
    .with(\.$star)
        .join(parent: \.$star)
    .fields(for: SpaceProgram.self)
    .fields(for: Star.self)
    .unique()


// apply filters here...


// Crashes on this line
let matchedPlanets = try await queryBuilder.paginate(for: req)

Crash text:

FluentKit/Siblings.swift:372: Fatal error: Unexpectedly found nil while unwrapping an Optional value
2023-08-29 20:33:13.217344-0700 Run[29204:1632081] FluentKit/Siblings.swift:372: Fatal error: Unexpectedly found nil while unwrapping an Optional value

Expected behavior

Should not crash, return results for pagination.

Environment

.package(url: "https://github.com/vapor/vapor.git", exact: "4.65.0"),
.package(url: "https://github.com/vapor/fluent.git", exact: "4.7.0"),
.package(url: "https://github.com/vapor/fluent-postgres-driver.git", exact: "2.6.0"),
  • OS version: macOS 13.5.1

Additional context

cc: @gwynne

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions