How to dynamically set a row value using data from a JS object? (onConflictDoUpdate)
I am:
- Checking if a row already exists
- If it doesn't: Insert the
data array's object
- If it exists: Update the price column - The issue is I'm not sure how to set a row to the relevant object in my data array
10 Replies
Drizzle ORM - Upsert Query
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Sorry which part of that?
To upsert multiple rows in one query in PostgreSQL and SQLite you can usesqloperator andexcludedkeyword.excludedis a special reference that refer to the row that was proposed for insertion, but wasn’t inserted because of the conflict. This is how you can do it:
Thanks I implemented it. Trying to fix
there is no unique or exclusion constraint matching the ON CONFLICT specification error nowDo you have a
unique constraint on organisationId and financialAccountId?@Sylvain For
financialAccountId yes it's the primaryKey. It isn't for organisationId but I tried removing that from target and it still persistsHmm, it should work so long as the conflict target is unique by its definition/constraints.
The values you're inserting also includes the
financialAccountId?yep, the values object:
The schema looks like it's properly constrained to me:
Yeah, I can't see any issue with that. I can't think of anything else off the top of my head, unfortunately.
Did you manage to solve this?