P
Prisma2mo ago
22parent

Multiple relations between the same Tables

I am trying to create the following relationship in my Prisma schema. I have two tables, Questions Answers I am trying to create the following relationship
model Question {
answers Answers[]
correctAnswer Answer
}

model Answer{
}
model Question {
answers Answers[]
correctAnswer Answer
}

model Answer{
}
How should i go about setting up the relations?
1 Reply
Nurul
Nurul2mo ago
Hey @22parent 👋 Did you consider something like this?
model Question {
id Int @id @default(autoincrement())
text String
answers Answer[] @relation("QuestionAnswers")
correctAnswer Answer @relation("CorrectAnswer", fields: [correctAnswerId], references: [id])
correctAnswerId Int @unique
}

model Answer {
id Int @id @default(autoincrement())
text String
question Question? @relation("QuestionAnswers", fields: [questionId], references: [id])
questionId Int?
correctFor Question? @relation("CorrectAnswer")
}
model Question {
id Int @id @default(autoincrement())
text String
answers Answer[] @relation("QuestionAnswers")
correctAnswer Answer @relation("CorrectAnswer", fields: [correctAnswerId], references: [id])
correctAnswerId Int @unique
}

model Answer {
id Int @id @default(autoincrement())
text String
question Question? @relation("QuestionAnswers", fields: [questionId], references: [id])
questionId Int?
correctFor Question? @relation("CorrectAnswer")
}