Update
update returns the record actually written and accepts select, include, and omit. Creating records is covered in Create, deletes in Delete.
Single Record
await db.user.update({
where: { id: user.id },
data: { name: "Alice Zhang" },
});2
3
4
wheremust match a unique key; a missing record raisesNOT_FOUNDand nothing is written.- Soft-deleted records are skipped: the call raises
NOT_FOUNDand the deletion field stays read-only, see Soft Delete.
upsert
const user = await db.user.upsert({
where: { email: "alice@example.com" },
create: { email: "alice@example.com" },
update: { name: { set: "Alice" } },
select: { email: true, name: true },
});2
3
4
5
6
| Strategy | When used |
|---|---|
| Single native statement | Only scalar fields are written; the unique key value in create equals the one in where; the model has no soft delete and no foreign key check against a soft-deleted model. On MySQL it additionally requires that the inserted record can only conflict on the unique key named by where, and that update leaves that key unchanged |
| Read-then-write in one transaction | All other cases, including nested writes and soft-deleted models |
The native statement is ON CONFLICT DO UPDATE on PostgreSQL (conflict target limited to the unique constraint named by where, so other unique conflicts still error), and ON DUPLICATE KEY UPDATE on MySQL, which fires on any unique key conflict. An upsert never restores a soft-deleted record: the unique key is still occupied, so it fails like any other write to a deleted record.
Concurrent creates
The native statement is concurrency-safe by database guarantee. The read-then-write path may hit unique or transaction conflicts on the first concurrent create; the ORM does not retry automatically, and callers can retry the whole transaction.
Atomic Field Updates
await db.post.update({
where: { id: 1 },
data: { views: { increment: 1 }, title: { set: "New title" } },
});
await db.post.updateMany({
where: { published: true },
data: { views: { multiply: 2 } },
});2
3
4
5
6
7
8
9
| Operation | Applicable fields |
|---|---|
set | All |
increment, decrement, multiply, divide | int, bigint, number, decimal |
- One operation per field, divisor nonzero;
intoperands must be safe integers,bigintacceptsbigint, an integer string, or a safe integer, and integer division truncates toward zero. decimaloperands may be a numeric string or a number; arithmetic runs in the database.NULLinvolved in an operation yieldsNULL; anintresult beyond the JavaScript safe-integer range raisesNUMERIC_OVERFLOWand rolls back the whole operation.
updateMany
const { count } = await db.post.updateMany({
where: { published: false },
data: { published: true },
});2
3
4
- Scalar fields only;
whereis required and non-empty (VALIDATIONotherwise), see Write Safety.$cleardeletes records instead of updating fields. countis matched records, including ones whose assignment changed nothing; soft-deleted records are never matched.- Normally one set-based
UPDATE; per-row when soft-delete foreign key validation or MySQL arithmetic checking applies, and withmaxAffectedmatched rows are counted first — a larger result raisesMASS_WRITE_BLOCKED.
updateManyAndReturn
const updated = await db.post.updateManyAndReturn({
where: { published: false },
data: { published: true, views: { increment: 1 } },
include: { author: { select: { email: true } } },
omit: { body: true },
});2
3
4
5
6
- Supports
select,include,omit, and virtual fields;whereis required and non-empty,{}raisesVALIDATION. - Return order is not guaranteed (sort by a business key yourself); records are the actual write results, returned even when the update changed a filter field, and empty update data returns the matched records.
Database Differences
| Capability | PostgreSQL | MySQL / MariaDB |
|---|---|---|
updateManyAndReturn | RETURNING | Select matching primary keys in a transaction, then update and read back per row |
Next Steps
- Create: write new records, including bulk inserts
- Delete: remove records instead of changing them
- Write Safety: the row limit applied to bulk updates