Nil value for required field of sibling relation in vapor / fluent causes fatal error

108 views Asked by At

I have a question about fluent / vapor. Maybe someone here has an idea and can help me.

I have a sibling relation which I want to eager-load when fetching an entity.

let all = try await ExerciseBase
    .query(on: req.db)
    .with(\.$allowedEquipments) // FIXME: Sibling: Error occurs
    .all()

The problem is that I get a fatal error with "field is not initalized or fetched". The query actually loads a nil value, although the field is declared as required and not optional. So the problem that causes the crash is clear, but I don't understand how a nil value can appear in the column at all. To my understanding this should already lead to an error when saving to the database if this column is non-optional or not null. The entries in the database are also correct, so there must be a problem somewhere when loading this relation.

Error

DB

I've really tried almost everything, checked the data, the database itself, the docker, the query...I can't find the problem and I'm running out of ideas.

struct CreateExerciseBaseEquipmentPivot: Migration {
    func prepare(on database: Database) -> EventLoopFuture<Void> {
        database.schema(ExerciseBaseEquipment.schema)
            .id()
            .field("exercise_base_id",
                   .uuid,
                   .required,
                   .references(ExerciseBase.schema, "id", onDelete: .cascade)
            )
            .field("equipment_id",
                   .uuid,
                   .required,
                   .references(Equipment.schema, "id", onDelete: .cascade)
            )
            .unique(on: "exercise_base_id", "equipment_id")
            .create()
    }

    func revert(on database: Database) -> EventLoopFuture<Void> {
        database.schema(ExerciseBaseEquipment.schema)
            .delete()
    }
}
final class ExerciseBaseEquipment: Model {
    static let schema = "exercise_base+equipments"

    @ID(key: .id)                       var id: UUID?
    @Parent(key: "exercise_base_id")    var exerciseBase: ExerciseBase
    @Parent(key: "equipment_id")        var equipment: Equipment

    init() {}

    init(id: UUID? = nil,
         exerciseBase: ExerciseBase,
         equipment: Equipment
    ) throws {
        self.id = id
        self.$equipment.id = try equipment.requireID()
        self.$exerciseBase.id = try exerciseBase.requireID()
    }
}
    @Siblings(
        through: ExerciseBaseEquipment.self,
        from: \.$exerciseBase,
        to: \.$equipment
    ) var allowedEquipments: [Equipment]
0

There are 0 answers