UPSERT operation not working for SQLite/Turso
Hey! So I'm trying to do a BULK UPSERT for multiple rows with a
.returning()
and its not working. I expect:
- UPDATE: rows which include the PKs "id"
- INSERT: rows which DO NOT include the PKs "id"
the code is as follows (I've followed the docs and tried some variants):
I would expect results being:
and I keep getting (the first row is unchanged / not updated):
Am I doing something wrong? Thanks! Any help is greatly appreciated!7 Replies
Hey @mrk! You try to update your
id
instead of name
. You should do smth like this:
Hey! Thanks for the reply! Hmm I understand what you mean, but what I'm trying to achieve is to run an update with ALL fields EXCEPT "id" field, so not just "name"... Is that possible? That is why I was attempting to "exclude" the "id" field
I also saw somewhere the possibility of doing something like the following:
But that wouldn't work for bulk upserts right @Mykhailo ? Which is what I'm trying to do...
Hmm, as I know there is no option to exclude column in sql, that's why we have to specify the columns we want to update. In guide we have custom function which can help to build upsert query, you can write smth like this but for excluding columns.
https://orm.drizzle.team/learn/guides/upsert
Also, we will add your case as an example in this guide with custom function for excluding columns.
Drizzle ORM - Upsert Query
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Thanks, makes sense! Adding it as en example would be great! I think many people might stumble with this use case. I'll try getting it to work and post the example here for the record 👍
Super, thank you!
So this is what I ended up doing (a very slight adaptation of the example currently in the docs), simply removing the PK,
"id"
in this case:
with
Thank you @mrk! I will test it and update guide then