onConflictDoUpdate array
Hi, is it possible to upsert/update an array?
I would want it to do it for all and not one by one
13 Replies
How would you write it in SQL?
I ended up doing one by one.
If you want the actual syntax I do this:
thanks yea that will be easier
Late to the party but what you want is For some reason it didn't work when I used templated column names after the EXCLUDED.
for templated column name, you need to use
sql.identifier(name)
like this: sql`excluded.${sql.identifier('first_name')}`
to take the name from the actual Drizzle column, you can write like this: sql.identifier(tblUserProfiles.firstName.name)
Very helpful. Do you think you could update the docs so that there is an array example? The upsert examles there are trivial.
what do you mean by array example?
Upserting multiple records where
db.insert().values()
take an array param rather than a single object.db.insert().values(items)
it takes an arrayBut can we set the value for
onConflictDoUpdate
based on the value that had a conflict when the values is an array?@MAST did you figure this out by any chance?
@alex you can do something like this:
or like this:
Works like a charm, thank you