KyselyK
Kysely3y ago
8 replies
Dska

Typing issue when working with onConflict

Hi, so i was trying to use some of examples https://kysely-org.github.io/kysely-apidoc/classes/InsertQueryBuilder.html#onConflict
I want to update column of json type in case of conflict
.onConflict((co) =>
                            co
                                .columns(['clinicId', 'eventGroupTag'])
                                .doUpdateSet((eb) => ({
                                    dayLimit: eb.ref('excluded.dayLimit')
                                })),
                        )

and get this error
TS2322: Type '(eb: ExpressionBuilder<OnConflictDatabase<DB, "clinicEventSetting">, OnConflictTables<"clinicEventSetting">>) => ExpressionWrapper<...>' is not assignable to type 'ValueExpression<OnConflictDatabase<DB, "clinicEventSetting">, OnConflictTables<"clinicEventSetting">, string | null> | undefined'.   Type '(eb: ExpressionBuilder<OnConflictDatabase<DB, "clinicEventSetting">, OnConflictTables<"clinicEventSetting">>) => ExpressionWrapper<...>' is not assignable to type 'OperandExpressionFactory<OnConflictDatabase<DB, "clinicEventSetting">, OnConflictTables<"clinicEventSetting">, string | null>'.     Type 'ExpressionWrapper<OnConflictDatabase<DB, "clinicEventSetting">, OnConflictTables<"clinicEventSetting">, JsonValue>' is not assignable to type 'OperandExpression<string | null>'.       Property 'isSelectQueryBuilder' is missing in type 'ExpressionWrapper<OnConflictDatabase<DB, "clinicEventSetting">, OnConflictTables<"clinicEventSetting">, JsonValue>' but required in type 'SelectQueryBuilderExpression<Record<string, string | null>>'.

However query goes ok



I guess that might be connected with https://github.com/RobinBlomberg/kysely-codegen because I am working with generated types
GitHub
Generate Kysely type definitions from your database! - GitHub - RobinBlomberg/kysely-codegen: Generate Kysely type definitions from your database!
GitHub - RobinBlomberg/kysely-codegen: Generate Kysely type definit...
Documentation for kysely
Solution
The issue is a mismatch between the insert and select types. That's essentially trying to assing
Json
to
string
. This is something Kysely should handle, but it's really difficult in that case. What I'd do is this

export type Json = ColumnType<
  JsonValue,
  JsonValue | string,
  JsonValue | string
>;


https://kyse.link/?p=s&i=uJp8OhW5wv4koycBFI8S
Was this page helpful?