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

1

16

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.

Conductivity answered 6/5, 2021 at 5:29 Comment(0)
C
34

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])
}
Coelenteron answered 13/5, 2021 at 20:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.