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 (将#修改为@)