Apply a conditional update
Change selected records
Drizzle ORM · Module
Drizzle’s update builder changes rows in a table using schema-typed values and an optional predicate. It supports parameterized values, SQL expressions, PostgreSQL returning, and dialect-specific clauses such as limit and orderBy.
Call update with a table, provide column values through set, and narrow affected rows with where. Values are parameterized automatically; PostgreSQL can return updated rows with returning, while SQL expressions can be supplied as update values.
await db.update(table).set(values).where(condition);| Name | Type | Description |
|---|---|---|
table | Table | Target table whose rows will be modified. |
set(values) | Update values | Object mapping schema column names to replacement values or SQL expressions. |
where(condition) | SQL condition | Predicate selecting rows to modify. |
An update query result; PostgreSQL can additionally return updated rows with returning().
import { eq } from 'drizzle-orm';
import { db } from './db';
import { users } from './schema';
const updated = await db
.update(users)
.set({ name: 'Mr. Dan' })
.where(eq(users.name, 'Dan'))
.returning({ updatedId: users.id });
console.log(updated);Updates matching users and returns their IDs in PostgreSQL.
Change selected records
Use a database-side calculation or function
Obtain identifiers or fields after modification
Use db.update(table).set(values).where(condition). The set object uses schema column names, and supplied values are parameterized automatically.
Follow-up: How can an update return changed rows? In PostgreSQL, append returning with the selected columns.
The keys passed to set must correspond to columns in the database schema. SQL expressions can also be used as values, such as sqlNOW() for a timestamp.
Follow-up: How would you update a timestamp using the database clock? Set the timestamp column to sqlNOW().
A PostgreSQL update can append returning to retrieve updated data, including a projection such as returning({ updatedId: users.id }). Use where to control which rows are changed.
Follow-up: What is the safety concern with returning? It retrieves affected rows but does not restrict which rows are modified; the predicate still belongs in where.
eqsqlreturninglimitorderByfrom