Enter data in a specific format into many to many relations tables using Prisma

14 views Asked by At

I use prisma with mysql database and I have three tables as follows

model product {
  id           Int     @id @default(autoincrement())
  sizes        sizes[]
}

model sizes {
  id        Int      @id @default(autoincrement())
  name      String
  quantity  String
  colors    colors[]
  product   product  @relation(fields: [productId], references: [id])
  productId Int
}

model colors {
  id       Int    @id @default(autoincrement())
  name     String
  quantity String
  sizes    sizes  @relation(fields: [sizesId], references: [id])
  sizesId  Int
}

Through a form, I receive data for sizes and colors in the following format

variants = [
  { name: "small", quantity: "10", colors: [{ name: "red", quantity: "5" }] },
  {
    name: "large",
    quantity: "10",
    colors: [
      { name: "red", quantity: "5" },
      { name: "blue", quantity: "5" },
    ],
  },
];

How can they be entered correctly, where the colors are entered and linked to the sizes ?

const product = await prisma.product.create({
    data: {
      sizes: {
        createMany: { data: [...variants] },   XXXXX
      },
    },
});

thank you

0

There are 0 answers