Relation Writes
Nested writes share one transaction with the parent write and the result projection; any failing step rolls back everything. Recursion is at most 8 levels deep.
Writing Inside create
create inputs support create, connect, and connectOrCreate; to-many direct foreign key relations also support createMany.
await db.user.create({
data: {
email: "alice@example.com",
posts: { create: [{ title: "First" }, { title: "Second" }] },
profile: { create: { bio: "Hello" } },
},
});
await db.post.create({
data: {
title: "Hello",
author: { connect: { email: "alice@example.com" } },
},
});
await db.post.create({
data: {
title: "Hello",
author: { connectOrCreate: { where: { email: "a@x.com" }, create: { email: "a@x.com" } } },
tags: { connectOrCreate: [{ where: { name: "ts" }, create: { name: "ts" } }] },
},
});
await db.user.create({
data: { email: "b@x.com", posts: { createMany: { data: [{ title: "One" }, { title: "Two" }] } } },
});2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
A required relation can be assigned through the foreign key field (such as authorId) or the relation field (such as author), but not both. connectOrCreate creates the record when the one named by where is missing; under concurrency a unique conflict is still possible.
Writing Inside update
await db.user.update({
where: { id: 1 },
data: {
name: "Alice",
profile: { update: { bio: "New bio" } },
posts: {
update: { where: { id: 10 }, data: { title: "Updated" } },
},
},
include: { profile: true, posts: true },
});
await db.post.update({
where: { id: 10 },
data: { tags: { set: [{ name: "TypeScript" }] } },
});2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Supported Operations
| Operation | To-one relation | To-many relation |
|---|---|---|
create | Create data for the target model | Create data or an array |
connect | Unique condition | Unique condition or array |
connectOrCreate | { where, create } | { where, create } or array |
update | Update data for the target model | { where, data } or array |
upsert | { create, update } | { where, create, update } or array |
delete | true | Unique condition or array |
disconnect | true, optional relations only | Unique condition or array, for detachable relations only |
set | — | Array of unique conditions |
updateMany | — | { where?, data } or array |
deleteMany | — | Filter condition or array |
createMany | — | { data, skipDuplicates? }, direct foreign key relations only |
A to-one relation may specify only one operation at a time. A to-many relation can combine multiple operations, and a single request can update multiple relation fields.
await db.user.update({
where: { id: 1 },
data: {
profile: { upsert: { create: { bio: "New" }, update: { bio: "Existing" } } },
posts: {
createMany: { data: [{ title: "Batch" }] },
updateMany: { where: { published: true }, data: { views: { increment: 1 } } },
deleteMany: { title: { startsWith: "obsolete" } },
},
},
});
await db.post.update({
where: { id: 1 },
data: { tags: { connectOrCreate: { where: { name: "TS" }, create: { name: "TS" } } } },
});2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Execution Order
Operations on the same to-many relation run in this order, each re-reading the current set of links: set → disconnect → delete → deleteMany → connect → connectOrCreate → create → createMany → update → updateMany → upsert
Array items in nested upsert also run sequentially, using the relation state left by earlier items. Internal membership and key queries are unaffected by global omit.
Rules
Scope
- For to-many relations,
update,delete, anddisconnectrequire each target to belong to the current relation: a missing target raisesNOT_FOUND, while one outside the relation raisesRELATION_NOT_CONNECTED. A to-oneupdatewith no related record raisesNOT_FOUND. - Nested
updateManyanddeleteManyare scoped to the parent relation. Unlike top-level bulk writes, nestedupdateManymay omitwhere, and nesteddeleteManyaccepts{}for that relation's visible records. - A nested
createcannot overwrite the parent record's foreign key, and a backconnectcannot move over an object that belongs to another parent record. - Forward polymorphic relations do not support nested writes. Inverse relations support
create/createManyincreate; inupdatethey accept the operations above exceptconnect,connectOrCreate,disconnect, andset, which raisePOLYMORPHIC_OPERATION. See Polymorphic Relations.
Disconnect and Delete
disconnectonly breaks the link;deleteremoves the target record. In a many-to-many relation,deletealso removes the target's other links.- A required foreign key cannot be set to
null, and a required to-one relation cannot be deleted;setdisconnects removed links first, so it likewise cannot leave a required foreign key null.
Bulk Operations
- Nested
updateManyaccepts only scalar updates;createManyworks on direct foreign key relations with scalar fields only.
Concurrent unique conflicts
connectOrCreate and nested upsert may fall back to read-then-write inside a transaction, so a concurrent unique conflict is still possible.
Next Steps
- Queries: relation filters and eager loading
- Polymorphic Relations: the operations a morph relation rejects
- Soft Delete: how nested writes treat deleted records