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

Potential typing bug with INSERT ON CONFLICT DO UPDATE WHERE #1189

Open
ethanresnick opened this issue Oct 21, 2024 · 0 comments
Open

Potential typing bug with INSERT ON CONFLICT DO UPDATE WHERE #1189

ethanresnick opened this issue Oct 21, 2024 · 0 comments

Comments

@ethanresnick
Copy link
Contributor

Hi, I have a query like:

kysely
  .insertInto('person')
  .values({ id, employeeNumber })
  .onConflict(oc =>
    oc.column('id').doUpdateSet(eb => ({
      employeeNumber: eb.ref('excluded.employeeNumber'),
    }))
    .where('person.employerId', '=', employerId)
  )
  .executeTakeFirst();

The intention is something like: upsert a person with their employee number, but only if the person to update is in the same company as the person performing the update. (This is not a real example from my domain, but you get the idea.)

The issue is that, in the types for the person table, the employer_id column's update type is never (as its intended to be immutable). However, kysely only lets me refer to the column in the where clause based on its update type, so the query gives an error when it seems like it should be allowed.

Self-contained example:

import { Kysely, ColumnType } from "kysely";

declare const db: Kysely<{
  person: {
    id: ColumnType<number, number, never>;
    employeeNumber: number;
    employerId:  ColumnType<number, number, never>;
  };
}>;

const result = db
  .insertInto('person')
  .values({ id: 32, employeeNumber: 4343, employerId: 1 })
  .onConflict(oc =>
    oc.column('id').doUpdateSet(eb => ({
      employeeNumber: eb.ref('excluded.employeeNumber'),
    }))
    .where('person.employerId', '=', 1)
  )
  .executeTakeFirst();

Error is:

Argument of type '"person.employerId"' is not assignable to parameter of type 'ReferenceExpression<OnConflictDatabase<{ person: { id: ColumnType<number, number, never>; employeeNumber: number; employerId: ColumnType<number, number, never>; }; }, "person">, OnConflictTables<...>>'
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

1 participant