Skip to content

Commit

Permalink
add workaround for nested createMany (#6496)
Browse files Browse the repository at this point in the history
* add workaround for nested createMany

* link workaround from reference
  • Loading branch information
nikolasburk authored Dec 5, 2024
1 parent 9c93616 commit e7d463c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ metaDescription: 'Prisma Client provides convenient queries for working with rel
toc_max_heading_level: 4
---

<TopBlock>

A key feature of Prisma Client is the ability to query [relations](/orm/prisma-schema/data-model/relations) between two or more models. Relation queries include:

- [Nested reads](#nested-reads) (sometimes referred to as _eager loading_) via [`select`](/orm/reference/prisma-client-reference#select) and [`include`](/orm/reference/prisma-client-reference#include)
Expand All @@ -15,8 +13,6 @@ A key feature of Prisma Client is the ability to query [relations](/orm/prisma-s

Prisma Client also has a [fluent API for traversing relations](#fluent-api).

</TopBlock>

## Nested reads

Nested reads allow you to read related data from multiple tables in your database - such as a user and that user's posts. You can:
Expand Down Expand Up @@ -242,7 +238,6 @@ const user = await prisma.user.findFirst({
</CodeWithResult>



### Select specific fields of included relations

You can use a nested `select` to choose a subset of fields of relations to return. For example, the following query returns the user's `name` and the `title` of each related post:
Expand Down Expand Up @@ -712,11 +707,39 @@ const result = await prisma.user.create({
</cmdResult>
</CodeWithResult>

<Admonition type="warning">
Note that it is **not possible** to nest an additional `create` or `createMany` inside the highlighted query, which means that you cannot create a user, posts, and post categories at the same time.

As a workaround, you can send a query to create the records that will be connected first, and then create the actual records. For example:

```ts
const categories = await prisma.category.createManyAndReturn({
data: [
{ name: 'Fun', },
{ name: 'Technology', },
{ name: 'Sports', }
],
select: {
id: true
}
});

const posts = await prisma.post.createManyAndReturn({
data: [{
title: "Funniest moments in 2024",
categoryId: categories[0].id
}, {
title: "Linux or macOS — what's better?",
categoryId: categories[1].id
},
{
title: "Who will win the next soccer championship?",
categoryId: categories[2].id
}]
});
```

**Note**: It is **not possible** to nest an additional `create` or `createMany` inside the highlighted query, which means that you cannot create a user, posts, and post categories at the same time.
If you want to create _all_ records in a single database query, consider using a [`$transaction`](/orm/prisma-client/queries/transactions#the-transaction-api) or [type-safe, raw SQL](/orm/prisma-client/using-raw-sql/typedsql).

</Admonition>

### Create multiple records and multiple related records

Expand Down
4 changes: 2 additions & 2 deletions content/200-orm/500-reference/050-prisma-client-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,7 @@ const deleteUser = await prisma.user.delete({

- As of Prisma ORM version 5.12.0, `createMany()` is now supported by SQLite.
- The `skipDuplicates` option is not supported by MongoDB, SQLServer, or SQLite.
- You **cannot** create or connect relations by using nested `create`, `createMany`, `connect`, `connectOrCreate` queries inside a top-level `createMany()` query.
- You **cannot** create or connect relations by using nested `create`, `createMany`, `connect`, `connectOrCreate` queries inside a top-level `createMany()` query. See here for a [workaround](/orm/prisma-client/queries/relation-queries#using-nested-createmany).
- You can use a nested [`createMany`](#createmany-1) query inside an [`update()`](#update) or [`create()`](#create) query - for example, add a `User` and two `Post` records with a nested `createMany` at the same time.

#### Examples
Expand Down Expand Up @@ -1272,7 +1272,7 @@ const users = await prisma.user.createMany({
#### Remarks

- The `skipDuplicates` option is not supported by SQLite.
- You **cannot** create or connect relations by using nested `create`, `createMany`, `connect`, `connectOrCreate` queries inside a top-level `createManyAndReturn()` query.
- You **cannot** create or connect relations by using nested `create`, `createMany`, `connect`, `connectOrCreate` queries inside a top-level `createManyAndReturn()` query. See here for a [workaround](/orm/prisma-client/queries/relation-queries#using-nested-createmany).
- When relations are included via `include`, a separate query is generated per relation.
- `relationLoadStrategy: join` is not supported.

Expand Down

0 comments on commit e7d463c

Please sign in to comment.