4

我试图在 Prisma ORM 的一个模型中建立两个 1:1 关系,但出现以下错误:

验证模型“人”时出错:检测到不明确的关系。字段placeOfBirthplaceOfDeath模型中Person均指Place. 请通过添加为它们提供不同的关系名称@relation(<name>)

我的棱镜架构:

model Place {
    id              Int     @id @default(autoincrement())
    name            String
    persons         Person[]
}

model Person {
    id              Int     @id @default(autoincrement())
    name            String
    placeOfBirthId  Int
    placeOfDeathId  Int
  placeOfBirth    Place   @relation(fields: [placeOfBirthId], references: [id])
    placeOfDeath    Place   @relation(fields: [placeOfDeathId], references: [id])
}

完全不明白。

4

1 回答 1

8

您必须在和中添加一个name字段。然后使用这些名称在模型中引用它们。placeOfBirthplaceOfDeathPlace

model Place {
  id     Int      @id @default(autoincrement())
  name   String
  Births Person[] @relation("Births")
  Deaths Person[] @relation("Deaths")
}

model Person {
  id             Int    @id @default(autoincrement())
  name           String
  placeOfBirthId Int
  placeOfDeathId Int
  placeOfBirth   Place  @relation("Births", fields: [placeOfBirthId], references: [id])
  placeOfDeath   Place  @relation("Deaths", fields: [placeOfDeathId], references: [id])
}
于 2021-05-13T20:24:54.583 回答