En este caso, un producto tiene muchas imágenes y también tiene una imagen principal.
Pero cuando prisma format , arroja un error:
error: Error validating field `image` in model `Product`: The relation field `image` on Model `Product` is missing an opposite relation field on the model `ProductImage`. Either run `prisma format` or add it manually. mi schema.prisma :
model ProductImage { id String @id @default(uuid()) @db.VarChar(36) product_id String product Product @relation(fields: [product_id], references: [id]) src String @db.VarChar(255) created_at DateTime @default(now()) updated_at DateTime @default(now()) @updatedAt @@map("product_image") } model Product { id String @id @default(uuid()) @db.VarChar(36) image_id String image ProductImage @relation(name: "main_image", fields: [image_id], references: [id]) images ProductImage[] slug String @unique(map: "slug") @db.VarChar(255) title String @db.VarChar(255) created_at DateTime @default(now()) updated_at DateTime @default(now()) @updatedAt @@map("product") }Estos modelos deberían resolver el problema:
model Product { id String @id @default(uuid()) @db.VarChar(36) image_id String @unique // image ProductImage @relation(name: "main_image", fields: [image_id], references: [id]) image ProductImage @relation(name: "main_image", fields: [image_id], references: [id], onDelete: NoAction, onUpdate: NoAction) images ProductImage[] slug String @unique(map: "slug") @db.VarChar(255) title String @db.VarChar(255) created_at DateTime @default(now()) updated_at DateTime @default(now()) @updatedAt @@map("product") } model ProductImage { id String @id @default(uuid()) @db.VarChar(36) src String @db.VarChar(255) created_at DateTime @default(now()) updated_at DateTime @default(now()) @updatedAt product_id String product Product @relation(fields: [product_id], references: [id]) product_main_image Product[] @relation("main_image") @@map("product_image") }Estas referencias a las relaciones en el esquema prisma deberían ser útiles.