Order Item

Finally, the OrderItem model, which becomes necessary as an intermediate entity between Product and Order due to custom properties. As can be noticed, we use @@id to indicate that it has a composite id.

model OrderItem {
  order     Order   @relation(fields: [orderId], references: [id], onDelete: Cascade)
  product   Product @relation(fields: [productId], references: [id])
  orderId   Int
  productId Int

  quantity Int
  price    Decimal @db.Decimal(6, 2)

  @@id([orderId, productId])
}

When saving the file, Prisma should automatically create the opposite side of the relation in Order and Product. Let's then just rename the field there and proceed.

items OrderItem[]

Now, generate and run the migration.

yarn migrate:create create-order-item
yarn migrate:run

Commit - Creating order item model

Last updated