如何在 Prisma 中链接多对多项目而不重复
我的产品和菜单之间有多对多的关系。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要从
MenuProducts
模型中删除重复项,您可以对product_id
和menu_id
的组合定义一个unique
约束,这将限制产品和菜单 ID 重复。这是
@@ 的参考唯一的
约束。To remove duplicates from the
MenuProducts
model you can define aunique
constraint on the combination ofproduct_id
andmenu_id
which will restrict having duplication of product and menu id.Here is a reference of
@@unique
constraint.