kysely-org / kysely

A type-safe typescript SQL query builder
https://kysely.dev
MIT License
9.82k stars 250 forks source link

OnConflict update all Fields #989

Closed ctaity-treinta closed 1 month ago

ctaity-treinta commented 1 month ago

Hi, i would like to kown if exists a way to do something like this:

const result = await db
  .insertInto('person')
  .values({
    first_name: 'Jennifer',
    last_name: 'Aniston',
    age: 40
  })
  .onConflict( (oc) => {
    oc.column("id").doMerge()
  }).execute()

and in doMerge, add all table fields, like doing an upsert, like knex merge, without need specify each field?

koskimas commented 1 month ago

on conflict do merge is not valid SQL.

AlexErrant commented 1 month ago

You might find this interesting/useful, where I add a type OnConflictUpdateCardSet so I get a type error if I add and don't handle a column: https://github.com/AlexErrant/Pentive/blob/0d3dc078952176a4cdbdeeb45253719f191eea43/app/src/sqlite/card.ts#L404-L410

ctaity-treinta commented 1 month ago

I think would be very useful have the columns to update all columns of the table, is a use case vert common.

AlexErrant commented 1 month ago

You can't update a PK, and kysely doesn't know what the PK is. There's no general solution for this so you'll need to write it yourself. Also, you probably won't want to update a createdAt column.

ctaity-treinta commented 1 month ago

but if i sent to insert { a, b, c ,d } and the conflict is on field a, you could update b, c, d, you have a point with createdAt, but also createdAt and updateAt columns usually are implemented as default values.

AlexErrant commented 1 month ago

Let me emphasize: kysely doesn't know what the PK is. Kysley is not an ORM.

and the conflict is on field a

That error occurs at runtime, which is not kysely's domain.