1

I'm having issues on how to seed a relationship of the following:

Store -< Product >- category

A store can have many products. A Category can have many products. My Issue is trying to seed store/products after category has been already seeded.

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
}

The problem I'm having is how do I seed this? I've tried doing something like the below code. I basically when I'm populating the store I take that store's name and apply it to a map. That map will push the full product object to the db. That mapped object already has done the mapping for the category:

// 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',
        product_category: consumerGoods,
        price: 399.99
      }
    ],
    Toys: [
      {
        name: 'Super Man',
        product_category: consumerGoods,
        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
        },
      },
    }))
  }

When I do it this way it complains:

 Unknown arg `id` in create.Product.create.0.product_category.id

How do I get past this error?

Mickey Sly
  • 429
  • 2
  • 6
  • 21

0 Answers0