无法在 Prisma 中的一个模型中创建两个一对一关系。检测到模糊关系。

14

我正在尝试在Prisma ORM的一个模型中创建两个1:1关系,但是遇到以下错误:

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

我的prisma架构:

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])
}

完全不理解。

1个回答

27
你需要在placeOfBirthplaceOfDeath中添加一个name字段,然后在Place模型中使用这些名称来引用它们。
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])
}

哦!原来如此!谢谢! - Nikolay Solovyov
谢谢,正是我所需要的。 - Prince Agrawal
那个 @relation("Births")Place 中就是我所遗漏的。谢谢!! - RaphaMex

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接