Prisma 一对多自关系

huangapple go评论76阅读模式
英文:

Prisma one to many self relation

问题

我正在查看Prisma文档,并尝试创建一个如下所示的一对多自关联模型:

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
}

根据我从文档中的阅读理解,这应该能够工作,因为他们有一个非常相似的示例。然而,我收到了以下错误:

错误:Prisma架构验证 - (get-dmmf wasm)
错误代码:P1012
错误:在模型`Shoe`中验证字段`variants`时出错:模型`Shoe`上的关联字段`variants`缺少模型`Shoe`上的相对关联字段。要么运行`prisma format`,要么手动添加它。

我需要一个具有主要sku的模型,并且需要将任何变种(例如不同尺寸)连接到它。

英文:

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.

答案1

得分: 0

我最终创建了一个 ShoeVariant 模型来存储只能更改的数据。

模型 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[]
}

模型 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
}

枚举 Gender {
Men
Women
}


我认为这是一个更好的方法,因为它不会保存重复的数据。
英文:

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.

huangapple
  • 本文由 发表于 2023年2月24日 00:31:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547676.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定