Multiple onConflictDoUpdate's supported?

I'm using two onConflictDoUpdate() and seems only the last one is picked up when run. Are multiple onConflictDoUpdate supported?
const [subscription] = await db
.insert(schema.subscriptions)
.values({
id: subscriptionData.id,
status: subscriptionData.status,
plan,
cycleStartedAt,
customerId: subscriptionData.customerId,
createdAt,
updatedAt,
})
.onConflictDoUpdate({
target: schema.subscriptions.id,
set: {
status: subscriptionData.status,
plan,
cycleStartedAt,
updatedAt,
},
setWhere: sql`${updatedAt.getTime()} > ${schema.subscriptions.updatedAt}`,
})
.onConflictDoUpdate({
target: schema.subscriptions.customerId,
set: {
id: subscriptionData.id,
status: subscriptionData.status,
plan,
cycleStartedAt,
createdAt,
updatedAt,
},
setWhere: sql`${createdAt.getTime()} > ${schema.subscriptions.createdAt}`,
})
.returning();
const [subscription] = await db
.insert(schema.subscriptions)
.values({
id: subscriptionData.id,
status: subscriptionData.status,
plan,
cycleStartedAt,
customerId: subscriptionData.customerId,
createdAt,
updatedAt,
})
.onConflictDoUpdate({
target: schema.subscriptions.id,
set: {
status: subscriptionData.status,
plan,
cycleStartedAt,
updatedAt,
},
setWhere: sql`${updatedAt.getTime()} > ${schema.subscriptions.updatedAt}`,
})
.onConflictDoUpdate({
target: schema.subscriptions.customerId,
set: {
id: subscriptionData.id,
status: subscriptionData.status,
plan,
cycleStartedAt,
createdAt,
updatedAt,
},
setWhere: sql`${createdAt.getTime()} > ${schema.subscriptions.createdAt}`,
})
.returning();
insert into "subscriptions" ("id", "status", "plan", "cycle_started_at", "customer_id", "created_at", "updated_at")
values (?, ?, ?, ?, ?, ?, ?)
on conflict ("subscriptions"."customer_id")
do update set "id" = ?, "status" = ?, "plan" = ?, "cycle_started_at" = ?, "created_at" = ?, "updated_at" = ?
where ? > "subscriptions"."created_at" returning "id", "status", "plan", "cycle_started_at", "customer_id", "created_at", "updated_at"
insert into "subscriptions" ("id", "status", "plan", "cycle_started_at", "customer_id", "created_at", "updated_at")
values (?, ?, ?, ?, ?, ?, ?)
on conflict ("subscriptions"."customer_id")
do update set "id" = ?, "status" = ?, "plan" = ?, "cycle_started_at" = ?, "created_at" = ?, "updated_at" = ?
where ? > "subscriptions"."created_at" returning "id", "status", "plan", "cycle_started_at", "customer_id", "created_at", "updated_at"
3 Replies
Sillvva
Sillvva7mo ago
Drizzle ORM - Upsert Query
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Sillvva
Sillvva7mo ago
There's an example in here You can't call it more than once, but you can use excluded to refer to rows that didn't get inserted due to a conflict.
flof.fly
flof.fly6mo ago
Super usefull thanks
Want results from more Discord servers?
Add your server