How to do "onConflictDoUpdate" when inserting an array of values?
I want to add multiple rows to my table but it has a unique key, so it has to update the row if there is a conflict. How do I do this with the ORM?
11 Replies
These are multiple ~2000 row inserts, so I don't think I want to wrap the map around the insert, that would be 10k+ insert queries
From sqlite docs: https://sqlite.org/lang_upsert.html
Column names in the expressions of a DO UPDATE refer to the original unchanged value of the column, before the attempted INSERT. To use the value that would have been inserted had the constraint not failed, add the special "excluded." table qualifier to the column name.So:
Ah yeah, "excluded" is something that copilot suggested but it added
sql.excluded
which wasnt a thing
Nice, thanks, will try thisBTW,
yourColumnName
should be the name of the column in the database, not the name in js
Say a column is name: text("name_column")
, your db column name is name_column
Got it! I think it's working. Thanks a lot!
I have a similar situation with composite unique index.
How can I use the index as the target?
I tried the above format:
But this results in an error:
I have verified that I am not passing any duplicate values in an array.
You have multiple indexes having conflicts and the DB doesn't like it
sql
excluded.yourColumnName
this does not work with postgres. It throws an error:
column excluded.yourColumnName does not exist
In my case, I am doing:
So, I get the error:
column excluded.text does not existMaybe your column
text
in ts is not called the same in the database
Can you show your posts table definitionOh, I have figured it out. I made a mistake in my post table definition
I am having this same issue, could you tell me how did you fix it?