onConflictDoUpdate array

Hi, is it possible to upsert/update an array?
await db.insert(organizations).values(organizationsResponse.organizations).onConflictDoUpdate({
target:[organizations.id],
set:{
name:organizationsResponse.organizations.map(org=>org.name)

}
});
await db.insert(organizations).values(organizationsResponse.organizations).onConflictDoUpdate({
target:[organizations.id],
set:{
name:organizationsResponse.organizations.map(org=>org.name)

}
});
I would want it to do it for all and not one by one
13 Replies
bloberenober
bloberenober14mo ago
How would you write it in SQL?
Antebios
Antebios13mo ago
I ended up doing one by one. If you want the actual syntax I do this:
async addUserProfile(userProfile: NewUserProfile) {
console.debug('useUsers.ts ==> addUserProfile ==> userProfile: ', userProfile);
const result = await this.db.insert(tblUserProfiles).values(userProfile)
.onConflictDoUpdate(
{
target: [tblUserProfiles.userId],
set: {
firstName: userProfile.firstName,
lastName: userProfile.lastName,
email: userProfile.email,
phoneNumber: userProfile.phoneNumber,
cellPhoneNumber: userProfile.cellPhoneNumber,
addressLine1: userProfile.addressLine1,
addressLine2: userProfile.addressLine2,
city: userProfile.city,
state: userProfile.state,
zipCode: userProfile.zipCode,
lastUpdateAt: new Date().toUTCString()
}
}
).returning();
async addUserProfile(userProfile: NewUserProfile) {
console.debug('useUsers.ts ==> addUserProfile ==> userProfile: ', userProfile);
const result = await this.db.insert(tblUserProfiles).values(userProfile)
.onConflictDoUpdate(
{
target: [tblUserProfiles.userId],
set: {
firstName: userProfile.firstName,
lastName: userProfile.lastName,
email: userProfile.email,
phoneNumber: userProfile.phoneNumber,
cellPhoneNumber: userProfile.cellPhoneNumber,
addressLine1: userProfile.addressLine1,
addressLine2: userProfile.addressLine2,
city: userProfile.city,
state: userProfile.state,
zipCode: userProfile.zipCode,
lastUpdateAt: new Date().toUTCString()
}
}
).returning();
Quea
Quea13mo ago
thanks yea that will be easier
Mr Redactle
Mr Redactle12mo ago
Late to the party but what you want is
set: { firstName: sql`EXCLUDED.first_name`, ... }
set: { firstName: sql`EXCLUDED.first_name`, ... }
For some reason it didn't work when I used templated column names after the EXCLUDED.
bloberenober
bloberenober12mo ago
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)
Mr Redactle
Mr Redactle12mo ago
Very helpful. Do you think you could update the docs so that there is an array example? The upsert examles there are trivial.
bloberenober
bloberenober12mo ago
what do you mean by array example?
Mr Redactle
Mr Redactle12mo ago
Upserting multiple records where db.insert().values() take an array param rather than a single object.
bloberenober
bloberenober12mo ago
db.insert().values(items) it takes an array
MAST
MAST12mo ago
But can we set the value for onConflictDoUpdate based on the value that had a conflict when the values is an array?
alex
alex9mo ago
@MAST did you figure this out by any chance?
MAST
MAST9mo ago
@alex you can do something like this:
onConflictDoUpdate({ title: sql`EXCLUDED.title` })
onConflictDoUpdate({ title: sql`EXCLUDED.title` })
or like this:
sql`excluded.${sql.identifier('first_name')}`
sql`excluded.${sql.identifier('first_name')}`
alex
alex9mo ago
Works like a charm, thank you
Want results from more Discord servers?
Add your server
More Posts
Relational queries for editing?Relational queries look great, but they are just for reading data right? Nothing like `updateOne` orIs there a way to dynamically edit a query with the builder?I am using drizzle and I love it, but, coming from knexjs, I am missing a lot this feature: https://When trying to push:mysql its warning that schema has changed and data will be lost when it hasnt.```· You're about to change projected_points column type from alter_table_alter_column_set_type to dInvalid input value for enumI have this enum definition as below and whenever I’m to make a request to save a data of the kind oShare migrations in monorepoI am using drizzle in a monorepo (npm workspaces with turborepo) within an internal package that is Top-level await is not available in the configured target environment ("node14")```typescript import type { Config } from "drizzle-kit"; import { getDbUrl } from "./src"; export How to migrate changes to a tableI'm trying to read up on drizzle and the first question that come to mind was how does migration worPostgres-js Migrate - Error: Can't find meta/_journal.json file [SOLVED]I've gotten drizzle setup and running but I'm unable to run any migrations. I'm using postgres-js aSveltekit Failed to resolve import "cloudflare:sockets"I was trying to use drizzle orm in a sveltekit project with postgres run in a docker container, but Unable to insert rows when using libsqlhey, i am using libsql on my local environment with drizzle but i am unable to insert anything in th