K
Kysely5mo ago
Joe

Raw SQL in where clause

I have the following code and I'm getting a type error. This is strange as the raw sql statement works for insert statements but doesn't seem to play nice with the where clause. In addition, the code below seems to work before 0.27.0.
await this.database
.get()
.selectFrom('fiat_country')
.where('disabled_at', '>', sql`now()`)
.selectAll()
.execute();
await this.database
.get()
.selectFrom('fiat_country')
.where('disabled_at', '>', sql`now()`)
.selectAll()
.execute();
error TS2345: Argument of type 'RawBuilder<unknown>' is not assignable to parameter of type 'OperandValueExpressionOrList<DB, "fiat_country", "disabled_at">'.
Property 'isSelectQueryBuilder' is missing in type 'RawBuilder<unknown>' but required in type 'SelectQueryBuilderExpression<Record<string, Date | null>>'.

78 .where('disabled_at', '>', sql`now()`)
~~~~~~~~~~
error TS2345: Argument of type 'RawBuilder<unknown>' is not assignable to parameter of type 'OperandValueExpressionOrList<DB, "fiat_country", "disabled_at">'.
Property 'isSelectQueryBuilder' is missing in type 'RawBuilder<unknown>' but required in type 'SelectQueryBuilderExpression<Record<string, Date | null>>'.

78 .where('disabled_at', '>', sql`now()`)
~~~~~~~~~~
With that said, if I cast the sql statement to never it seems to resolve the problem. But I don't think that's the proper fix.
await this.database
.get()
.selectFrom('fiat_country')
.where('disabled_at', '>', sql`now()` as never)
.selectAll()
.execute();
await this.database
.get()
.selectFrom('fiat_country')
.where('disabled_at', '>', sql`now()` as never)
.selectAll()
.execute();
Here's what I'm using: kysely: 0.27.2 kysely-codegen: 0.12.0 Appreciate it if anyone have any insides to this! 🙏🏻
Solution:
Give a type for the raw expression
.where('disabled_at', '>', sql<Date>`now()`)
.where('disabled_at', '>', sql<Date>`now()`)
...
Jump to solution
1 Reply
Solution
koskimas
koskimas5mo ago
Give a type for the raw expression
.where('disabled_at', '>', sql<Date>`now()`)
.where('disabled_at', '>', sql<Date>`now()`)