如何解决Prisma 如何创建对数据模型的多个引用
我在如何建立以下关系方面遇到问题:
商店 -- 类别
一个商店可以有很多产品。一个类别可以有许多产品。我的问题正在尝试在类别已经播种后播种商店/产品。
model Store {
id String @id @default(uuid())
name String @unique
Product Product[]
}
model Category {
id String @id
name String @unique
Product Product[]
}
model Product {
id String @default(uuid())
name String @unique
creator Store @relation(fields: [store_id],references: [id])
store_id String // relation to creator field
product_category Category @relation(fields: [category_id],references: [id])
category_id String // relation to product_category field
price Number
}
我遇到的问题是如何播种?我试过做类似下面的代码的事情。基本上,当我填充商店时,我会使用该商店的名称并将其应用于地图。该映射会将完整的产品对象推送到数据库。该映射对象已经完成了类别的映射:
// get the category that i want to seed
const consumerGoods = await prisma.category.findUnique({
where: {
id: 'CS',},})
const productMap = {
Sports: [
{
name: 'basketball',product_category: consumerGoods,// store category in storemap
price: 35.95
}
],Games: [
{
name: 'ps5',price: 399.99
}
],Toys: [
{
name: 'Super Man',price: 14.50
}
]
}
for (const name of storeNames) {
institutions.push(await prisma.store.upsert({
where: { name},update: {},create: {
name: name,Product: {
create: productMap[name] // place complete object in create
},}))
}
当我这样做时,它会抱怨:
Unknown arg `id` in create.Product.create.0.product_category.id
我该如何克服这个错误?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。