K
Kysely6mo 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)
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'),
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),
)
}
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 >
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
}>()
.$narrowType<PrzyjecieZewnetrznePozycjaRozbicieSelect & {
stawkaVatZakupu: NotNull
}>()
any clue?
Relations | Kysely
Kysely IS NOT an ORM. Kysely DOES NOT have the concept of relations.
Solution:
Never use undefined or ? in your type interfaces.
Jump to solution
2 Replies
Solution
koskimas
koskimas6mo ago
Never use undefined or ? in your type interfaces.
koskimas
koskimas6mo ago
Alternatively the issue might be that stawkaVatZakupuId is nullable. You can't assign it to Expression<number> If you know stawkaVatZakupuId is not null, assert the reference. Not the return value of the query.
ref('rozbicie.stawkaVatZakupuId').$notNull()
ref('rozbicie.stawkaVatZakupuId').$notNull()

Did you find this page helpful?