In this case, a Product has many Image , and also has one main Image .
But when i run prisma format
,throw 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.
my 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")
}
This models should solve the issue:
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")
}
These references to relations in prisma schema should be helpful.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.