简体   繁体   中英

Can't make two 1:1 relations in one model in Prisma. Ambiguous relation detected

I'm trying to make two 1:1 relations in one model in Prisma ORM, but got following error:

Error validating model "Person": Ambiguous relation detected. The fields placeOfBirth and placeOfDeath in model Person both refer to Place . Please provide different relation names for them by adding @relation(<name>) .

My prisma schema:

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

Totally don't get it.

You have to add a name field to placeOfBirth and placeOfDeath . Then use these names to reference both of them in the Place model.

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM