Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create nested entities #10333

Open
alechko opened this issue Nov 18, 2021 · 3 comments
Open

Create nested entities #10333

alechko opened this issue Nov 18, 2021 · 3 comments
Labels
kind/feature A request for a new feature. team/client Issue for team Client. topic: create() topic: create nested create topic: nested write (create/update/delete) https://www.prisma.io/docs/concepts/components/prisma-client/relation-queries#nested-writes topic: relations

Comments

@alechko
Copy link

alechko commented Nov 18, 2021

Problem

Currently it's not possible to create nested entities within a single operation, for example, consider a scenario of seeding a User with a Post and a Comment with children Comments (replies):

    await prisma.user.create({
      data: {
        email: faker.internet.email(),
        posts: {
          create: {
            title: faker.lorem.sentence(10),
            comments: {
              create: [
                {
                  body: faker.lorem.paragraph(10),
                  children: {
                    create: [ <--- THIS FAILS TO INHERIT THE POST
                      {
                        body: faker.lorem.paragraph(10),
                      }
                    ]
                  },
                },
              ],
            },
          },
        },
      },
    });

Comment model:

model Comment {
  id          String          @id() @default(uuid()) @db.Uuid
  body        String

  post        Post            @relation(fields: [postId], references: [id], onDelete: Cascade)
  postId      String          @db.Uuid

  parent      Comment?        @relation("CommentParent", fields: [parentId], references: [id])
  parentId    String?         @db.Uuid
  children    Comment[]       @relation("CommentParent")
}

Suggested solution

It could be helpful if nested create would inherit the parent entity fields/relations instead of manually creating and relating entities.

@janpio janpio added kind/improvement An improvement to existing feature and code. team/client Issue for team Client. labels Nov 19, 2021
@ahsansher
Copy link

ahsansher commented Oct 19, 2022

It will be a huge timesaver but still no discussion on it.

@zane-commeatio

This comment was marked as off-topic.

@fedesn

This comment was marked as off-topic.

@janpio janpio added topic: nested write (create/update/delete) https://www.prisma.io/docs/concepts/components/prisma-client/relation-queries#nested-writes topic: relations kind/feature A request for a new feature. and removed kind/improvement An improvement to existing feature and code. labels Jun 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature A request for a new feature. team/client Issue for team Client. topic: create() topic: create nested create topic: nested write (create/update/delete) https://www.prisma.io/docs/concepts/components/prisma-client/relation-queries#nested-writes topic: relations
Projects
None yet
Development

No branches or pull requests

5 participants