如何在Prisma中保存树结构数据

发布于 2025-02-13 13:37:46 字数 1593 浏览 0 评论 0原文

我尝试使用带有Prisma的Nestjs构建应用程序,我的树结构保存数据有问题,这是我的类别模型。

model Category {
  id   Int    @id @default(autoincrement())
  name String

  children Category[] @relation("SubCategories")
  parent   Category?  @relation("SubCategories", fields: [parentId], references: [id])
  parentId Int?

  Product Product[]

  @@map("categories")
}

这是我的逻辑:

async createCategory(dto: createCategoryDto) {
    const newCategory = await this.prisma.category.create({
        data: {
            name: dto.name,
            parent: dto.parentId
        }
    });
    return newCategory;
}

但是当我试图保存数据时,我会收到此错误:

    ...
    [Nest] 30699  - 07/06/2022, 1:31:36 PM   ERROR [ExceptionsHandler] 
    Invalid `this.prisma.category.create()` invocation in
    /home/fadhli/SandBox/lanjo/lanjo-api/src/category/category.service.ts:26:56
    
      23 }
      24 
      25 async createCategory(dto: createCategoryDto) {
    → 26     const newCategory = await this.prisma.category.create({
               data: {
                 name: 'example',
                 parentId: '1'
                 ~~~~~~~~
               }
             })
    
    Unknown arg `parentId` in data.parentId for type CategoryCreateInput. Did you mean `parent`? Available args:
    type CategoryCreateInput {
      name: String
      children?: CategoryCreateNestedManyWithoutParentInput
      parent?: CategoryCreateNestedOneWithoutChildrenInput
      Product?: ProductCreateNestedManyWithoutCategoryInput
    }

如何将树数据保存到Nestjs和Prisma中的数据库中?

谢谢。

i try to build app with nestjs with prisma, i have problem with tree structure saving the data, here is my model for categories.

model Category {
  id   Int    @id @default(autoincrement())
  name String

  children Category[] @relation("SubCategories")
  parent   Category?  @relation("SubCategories", fields: [parentId], references: [id])
  parentId Int?

  Product Product[]

  @@map("categories")
}

this is my logic:

async createCategory(dto: createCategoryDto) {
    const newCategory = await this.prisma.category.create({
        data: {
            name: dto.name,
            parent: dto.parentId
        }
    });
    return newCategory;
}

but when i am trying to save data, i got this error:

    ...
    [Nest] 30699  - 07/06/2022, 1:31:36 PM   ERROR [ExceptionsHandler] 
    Invalid `this.prisma.category.create()` invocation in
    /home/fadhli/SandBox/lanjo/lanjo-api/src/category/category.service.ts:26:56
    
      23 }
      24 
      25 async createCategory(dto: createCategoryDto) {
    → 26     const newCategory = await this.prisma.category.create({
               data: {
                 name: 'example',
                 parentId: '1'
                 ~~~~~~~~
               }
             })
    
    Unknown arg `parentId` in data.parentId for type CategoryCreateInput. Did you mean `parent`? Available args:
    type CategoryCreateInput {
      name: String
      children?: CategoryCreateNestedManyWithoutParentInput
      parent?: CategoryCreateNestedOneWithoutChildrenInput
      Product?: ProductCreateNestedManyWithoutCategoryInput
    }

how to save tree data into database in nestjs and prisma?

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

优雅的叶子 2025-02-20 13:37:46

您需要指定,关系应如何发生:您要连接现有记录parent,还是要创建>创建 IT IT IT内线。例如:

const newCategory = await this.prisma.category.create({
    data: {
        name: dto.name,
        parent: {
            connect: {
                id: dto.parentId,
            },
        },
    }
});

You need to specify, how relation should occur: do you want to connect an existing record as parent, or do you want to create it inline. For example:

const newCategory = await this.prisma.category.create({
    data: {
        name: dto.name,
        parent: {
            connect: {
                id: dto.parentId,
            },
        },
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文