如何在 Prisma 中链接多对多项目而不重复

发布于 2025-01-09 07:46:42 字数 1353 浏览 1 评论 0原文

我的产品和菜单之间有多对多的关系。

model Product {
  id    Int    @id @default(autoincrement())
  name String
  price Float
  description String?
  product_category ProductCategory @relation(fields: [category_id], references: [id])
  category_id Int

  menus MenuProducts[]

  created_at DateTime @default(now())
  updated_at DateTime @updatedAt

  @@map("products")
}

model Menu {
  id    Int    @id @default(autoincrement())
  name String
  products MenuProducts[]

  created_at DateTime @default(now())
  updated_at DateTime @updatedAt
  @@map("menus")

}

model MenuProducts {
  menu_id Int
  menu Menu @relation(fields: [menu_id], references: [id])

  product_id Int
  product Product @relation(fields: [product_id], references: [id])


  created_at DateTime @default(now())
  updated_at DateTime @updatedAt

  @@map("menu_products")

  @@id([product_id, menu_id])
}

在菜单更新中,我想将许多产品分配给菜单。

我已经尝试过这样的操作:

this.prisma.menu.update({where: {id}, data: {...updateMenuDto, products: {connect: {id: 6}}}});

但收到此错误:

未找到所需的连接记录。预计在一对多关系“MenuToMenuProducts”上进行连接操作后会连接 1 条记录,发现 0。

无论如何,我已经尝试过这样的操作:

this.prisma.menuProducts.create({data:{menu: {connect: {id: id}},product: {connect: {id: 6}}}})

并且它有效,但在第二次调用时我看到了该行被重复。如何将多个产品链接到菜单而不插入重复项?

I have many to many relationships between Product and Menu.

model Product {
  id    Int    @id @default(autoincrement())
  name String
  price Float
  description String?
  product_category ProductCategory @relation(fields: [category_id], references: [id])
  category_id Int

  menus MenuProducts[]

  created_at DateTime @default(now())
  updated_at DateTime @updatedAt

  @@map("products")
}

model Menu {
  id    Int    @id @default(autoincrement())
  name String
  products MenuProducts[]

  created_at DateTime @default(now())
  updated_at DateTime @updatedAt
  @@map("menus")

}

model MenuProducts {
  menu_id Int
  menu Menu @relation(fields: [menu_id], references: [id])

  product_id Int
  product Product @relation(fields: [product_id], references: [id])


  created_at DateTime @default(now())
  updated_at DateTime @updatedAt

  @@map("menu_products")

  @@id([product_id, menu_id])
}

In the menu update, I want to assign many products to the menu.

I've tried like this:

this.prisma.menu.update({where: {id}, data: {...updateMenuDto, products: {connect: {id: 6}}}});

but I received this error:

The required connected records were not found. Expected 1 records to be connected after connect operation on one-to-many relation 'MenuToMenuProducts', found 0.

Anyway, I've tried like this:

this.prisma.menuProducts.create({data:{menu: {connect: {id: id}},product: {connect: {id: 6}}}})

and it worked, but on the second call I saw what the line was duplicated. How I can link multiple products to menu without inserting duplicates?

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

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

发布评论

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

评论(1

南薇 2025-01-16 07:46:42

要从 MenuProducts 模型中删除重复项,您可以对 product_idmenu_id 的组合定义一个 unique 约束,这将限制产品和菜单 ID 重复。

model MenuProducts {
  menu_id Int
  menu Menu @relation(fields: [menu_id], references: [id])

  product_id Int
  product Product @relation(fields: [product_id], references: [id])


  created_at DateTime @default(now())
  updated_at DateTime @updatedAt

  @@map("menu_products")

  @@id([product_id, menu_id])

  @@unique([product_id, menu_id])
}

这是 @@ 的参考唯一的约束。

To remove duplicates from the MenuProducts model you can define a unique constraint on the combination of product_id and menu_id which will restrict having duplication of product and menu id.

model MenuProducts {
  menu_id Int
  menu Menu @relation(fields: [menu_id], references: [id])

  product_id Int
  product Product @relation(fields: [product_id], references: [id])


  created_at DateTime @default(now())
  updated_at DateTime @updatedAt

  @@map("menu_products")

  @@id([product_id, menu_id])

  @@unique([product_id, menu_id])
}

Here is a reference of @@unique constraint.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文