Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small possible issue with Typescript definitions in onConflict when using knex.raw #4518

Open
fkaempfer opened this issue Jun 9, 2021 · 1 comment

Comments

@fkaempfer
Copy link

fkaempfer commented Jun 9, 2021

Environment

Knex version: 0.95.6
Database + version: PostgreSQL 13
OS: Linux

If issue is about TypeScript definitions, tag @lorefnon

Bug / Maybe Feature request

  1. Explain what kind of behaviour you are getting and how you think it should do
    In PostgreSQL it is possible to use expressions in the ON CONFLICT target clause, for instance ON CONFLICT ("name", COALESCE(table_id, -1)). This is particularily useful when wanting to have unique indexes that treat NULL as one specific value (for example CREATE UNIQUE INDEX etst_idx ON public.my_table (id1, coalesce(other_table_id, -1)); This index would not allow duplicate rows with id1 and null

In Knex it is already possibly to create on conflict clauses like that using .onConflict(['id', knex.raw('coalesce(other_table_id, -1)'). This code, intended or not, does create an ON CONFLICT clause that references this index which is very nice! However this is not intended by the Typescript definitions at least, so you have to write as unknown as string otherwise you get the compiler error message below.

  1. Error message
    TS error:
  The last overload gave the following error.
    Type 'Raw<any>' is not assignable to type 'string'.
  1. Reproducer

Works:

knex('my_table').insert([]).onConflict(['my_id', knex.raw('COALESCE(id2,-1)') as unknown as string])

Doesnt Work:

knex('my_table').insert([]).onConflict(['my_id', knex.raw('COALESCE(id2,-1)')])
@rijkvanzanten
Copy link
Collaborator

The same problem applies to a couple other places where .raw can be used as a drop-in replacement for a string identifier, like all the where* methods

knex.select('*').where(knex.raw('example')); // error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants