K
Kysely13mo ago
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')
})),
)
.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>>'.
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
GitHub - RobinBlomberg/kysely-codegen: Generate Kysely type definit...
Generate Kysely type definitions from your database! - GitHub - RobinBlomberg/kysely-codegen: Generate Kysely type definitions from your database!
InsertQueryBuilder | kysely
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 ```ts export type Json = ColumnType< JsonValue,...
Jump to solution
7 Replies
Dska
DskaOP13mo ago
Also if i ignore first error here is next
      The types of 'dayLimit' are incompatible between these types.         Type 'ExpressionWrapper<OnConflictDatabase<DB, "clinicEventSetting">, OnConflictTables<"clinicEventSetting">, JsonValue>' is not assignable to type 'ValueExpression<OnConflictDatabase<DB, "clinicEventSetting">, OnConflictTables<"clinicEventSetting">, string | null> | undefined'.           Type 'ExpressionWrapper<OnConflictDatabase<DB, "clinicEventSetting">, OnConflictTables<"clinicEventSetting">, JsonValue>' is not assignable to type 'SelectQueryBuilderExpression<Record<string, string | null>>'.             The types of 'toOperationNode().kind' are incompatible between these types.               Type 'OperationNodeKind' is not assignable to type '"SelectQueryNode"'.                 Type '"IdentifierNode"' is not assignable to type '"SelectQueryNode"'.
      The types of 'dayLimit' are incompatible between these types.         Type 'ExpressionWrapper<OnConflictDatabase<DB, "clinicEventSetting">, OnConflictTables<"clinicEventSetting">, JsonValue>' is not assignable to type 'ValueExpression<OnConflictDatabase<DB, "clinicEventSetting">, OnConflictTables<"clinicEventSetting">, string | null> | undefined'.           Type 'ExpressionWrapper<OnConflictDatabase<DB, "clinicEventSetting">, OnConflictTables<"clinicEventSetting">, JsonValue>' is not assignable to type 'SelectQueryBuilderExpression<Record<string, string | null>>'.             The types of 'toOperationNode().kind' are incompatible between these types.               Type 'OperationNodeKind' is not assignable to type '"SelectQueryNode"'.                 Type '"IdentifierNode"' is not assignable to type '"SelectQueryNode"'.
Dska
DskaOP13mo ago
My Kysely version is 0.26.3 I see that working with json ref is now supported https://github.com/kysely-org/kysely/pull/440
GitHub
add json traversal support. by igalklebanov · Pull Request #440 · k...
Trying to figure out what we can do, what we should do, and how this can be scoped and explained. JSON path syntax is supported by all 3 built-in dialects. So having a type-safe JSON path builder ...
Igal
Igal13mo ago
Hey 👋 Can you provide a kyse.link with the generated types and the query you're trying to build?
Dska
DskaOP12mo ago
Playground Link @Igal thanks for response
Solution
koskimas
koskimas12mo ago
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
>;
export type Json = ColumnType<
JsonValue,
JsonValue | string,
JsonValue string
>;
https://kyse.link/?p=s&i=uJp8OhW5wv4koycBFI8S
Dska
DskaOP12mo ago
@koskimas thank you, do you manually override generated types or kysely codegen provides an interface to configure some basic types ?
koskimas
koskimas12mo ago
I don't know. I never use codegens for kysely types.
Want results from more Discord servers?
Add your server