Reputation: 192
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
andplaceOfDeath
in modelPerson
both refer toPlace
. 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.
Upvotes: 16
Views: 8711
Reputation: 874
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])
}
Upvotes: 34