Skip to content

Batch Updates / Update Many #2557

Discussion options

You must be logged in to vote

Seems simpler to run upsert, as insert supports multirow. This is my solution:

...
  await db
    .insert(Table)
    .values(values)
    .onConflictDoUpdate({
      target: Table.id,
      set: conflictUpdateSetAllColumns(Table, ["id"]),
    })
    .returning()
// src/db/helpers.ts
import { sql } from "drizzle-orm"
import { PgUpdateSetSource, PgTable } from "drizzle-orm/pg-core"
import { getTableColumns } from "drizzle-orm"
import { getTableConfig } from "drizzle-orm/pg-core"

export function conflictUpdateSetAllColumns<
  T extends PgTable,
  E extends (keyof T["$inferInsert"])[],
>(table: T, except?: E): PgUpdateSetSource<T> {
  const columns = getTableColumns(table)
  const config = ge…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ryanking1809
Comment options

Answer selected by ryanking1809
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants