Targeted updates
Change selected fields on rows matching a condition.
Drizzle ORM · Module
.set() defines the column assignments for a Drizzle ORM update query. It accepts schema-column keys with values or SQL expressions, and can be combined with conditions and returning clauses.
.set() supplies the column-value assignments for an update query. Pass an object whose keys correspond to schema columns; values may be ordinary data or SQL expressions, and the completed update can be narrowed with clauses such as .where() or extended with .returning().
db.update(table).set({ column: value })| Name | Type | Description |
|---|---|---|
set | Record<keyof TTable, value | SQL> | An object mapping schema column names to replacement values or SQL expressions. |
An update query builder; execution returns the database driver's update result, while supported dialects can use .returning() to retrieve updated rows or projections.
.returning() to retrieve selected values from changed rows..limit() can restrict the number of affected rows..orderBy() for ordering rows considered by the update.undefined property is skipped rather than written; use null when the intended database value is NULL.import { eq } from "drizzle-orm";
import { users } from "./schema";
await db.update(users)
.set({ name: "Mr. Dan" })
.where(eq(users.name, "Dan"));Updates the matching user's name; the WHERE clause limits which rows are changed.
Change selected fields on rows matching a condition.
Set timestamps, counters, or other values calculated by the database.
Retrieve identifiers or projections from changed rows where the dialect supports it.
.set() do in an update query?.set() receives an object containing the columns to change and their new values. In db.update(users).set({ name: "Mr. Dan" }), Drizzle creates the assignment for name.
Follow-up: How would you assign a database-computed value? Pass a SQL expression, such as sql\NOW()\, as the property value.
.set()?Values supplied to .set() are parameterized automatically. This keeps ordinary values separate from the generated SQL rather than interpolating them directly.
Follow-up: How can a value be computed from an existing column? Use a SQL value such as sql\${table.counter} + 1\.
.set() value is undefined?A property with undefined is ignored, so it does not overwrite the column. To write SQL NULL, provide null explicitly.
Follow-up: What should be used when an update should affect only selected rows? Add a .where() condition to constrain the update.
updatewherereturningfromsqllimitorderBy