Search code examples
ormschemadatabase-schemaprismaprisma2

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.


Solution

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