Reputation: 59
I'm looking at the Prisma docs and trying to create a one-to-many self relation as in the model below:
model Shoe {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
sku String @unique
model String @db.VarChar(50)
description String @db.VarChar(256)
image String @db.VarChar(256)
category String @db.VarChar(256)
gender Gender
size String
stock Int
variants Shoe[] @relation("variants")
}
enum Gender {
Men
Women
}
As I understand from reading the docs this should work as they have a very similar example, however, I get the error below:
Error: Prisma schema validation - (get-dmmf wasm)
Error code: P1012
error: Error validating field `variants` in model `Shoe`: The relation field `variants` on model `Shoe` is missing an opposite relation field on the model `Shoe`. Either run `prisma format` or add it manually.
I need to have a model with a main sku and any variants of it, e.g. different size, need to be connected to it.
Upvotes: 0
Views: 994
Reputation: 59
I ended up creating a ShoeVariant model to hold the data that can change only.
model Shoe {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
sku String @unique
model String @db.VarChar(50)
description String @db.VarChar(256)
image String @db.VarChar(256)
category String @db.VarChar(256)
gender Gender
size String
stock Int
variants ShoeVariant[]
}
model ShoeVariant {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
sku String @unique
size String
stock Int
Shoe Shoe @relation(fields: [shoeId], references: [id])
shoeId Int
}
enum Gender {
Men
Women
}
I'd say this is a better approach since it does not hold duplicated data.
Upvotes: 0