Ryan Prior
Ryan Prior
PPrisma
Created by Ryan Prior on 7/21/2024 in #help-and-questions
Contacts: error combining many-to-one with one-to-one on the same models
I'm just getting started modeling with Prisma and quickly hit a case I can't find help for in the docs. I want a Person to be able to have any number of Contacts, but exactly one primary Contact. My incorrect model looks like this:
model Contact {
id Int @id @default(autoincrement())
email String
personId Int
person Person @relation("PersonContact")
}

model Person {
id Int @id @default(autoincrement())
contactId Int
primaryContact Contact @relation("PersonPrimaryContact")
contacts Contact[] @relation("PersonContact")
}
model Contact {
id Int @id @default(autoincrement())
email String
personId Int
person Person @relation("PersonContact")
}

model Person {
id Int @id @default(autoincrement())
contactId Int
primaryContact Contact @relation("PersonPrimaryContact")
contacts Contact[] @relation("PersonContact")
}
If I remove the primaryContact field, this validates fine, but it's giving confusing errors with the primary contact, saying that model Contact lacks a relation back to Person. This is clearly not the case. What's wrong with my approach, and how would you proceed?
4 replies