KyselyK
Kysely2y ago
tzezar

json object relation null problem

I am trying to make https://kysely.dev/docs/recipes/relations this helper work, but get error
Argument of type 'ExpressionWrapper<DB & { pozycja: DokumentPozycja; } & { rozbicie: DokumentPozycjaRozbicie; }, "dokument" | "pozycja" | "rozbicie", number | null>' is not assignable to parameter of type 'Expression<number>'.
  Types of property 'expressionType' are incompatible.
    Type 'number | null | undefined' is not assignable to type 'number | undefined'.
      Type 'null' is not assignable to type 'number | undefined'.ts(2345)

I wrote code following docs example:

usage:
stawka(ref('rozbicie.stawkaVatZakupuId')).$notNull().as('stawkaVatZakupu'),

and helper expression?:
function stawka(stawkaVatId: Expression<number >) {
    return jsonObjectFrom(
        db.selectFrom('stawkaVat')
            .select([
                'stawkaVat.id',
                'stawkaVat.nazwa',
                'stawkaVat.wartosc'
            ])
            .where('stawkaVat.id', '=', stawkaVatId),
    )
}

adding union with null here:
Expression<number | null >

fixes problem, but it looks like it should work without null after adding .$notNull() as above and later after select
   .$narrowType<PrzyjecieZewnetrznePozycjaRozbicieSelect & {
                                        stawkaVatZakupu: NotNull
                                    }>()
any clue?
Kysely IS NOT an ORM. Kysely DOES NOT have the concept of relations.
Solution
Never use undefined or ? in your type interfaces.
Was this page helpful?