1
votes

I have a User model along UserRelationship model

I have created trainer/client relationship with the UserRelationship model.

model User {
  id        Int      @id @default(autoincrement())
  trainerRelationship UserRelationship[] @relation("trainer")
  traineeRelationship UserRelationship[] @relation("trainee")
}
model UserRelationship {
  id          Int       @id @default(autoincrement())
  trainerId   Int?
  traineeId   Int?
  trainerUser User?  @relation("trainer", fields: [trainerId], references: [id])
  traineeUser User?  @relation("trainee", fields: [traineeId], references: [id])

}

I am able to query the user also able to query their relationship

For eg:

  const trainer = await client.user
    .findUnique({ where: { id }, include: { trainerRelationship: true } })

The output i am getting here is like:

{
  id: 1,
  username: hellouser
  trainerRelationship: [
    {

      id: 4,
      trainerId: 1,
      traineeId: 34
    }
]
}

Now i want to fetch the details of the traineeId/trainerId user details, What should be my query?

1

1 Answers

1
votes

Ok, so I have solved it by the below query, incase someone is stuck:

.findUnique({ where: { id } }).trainerRelationship({
  include: {
    trainerUser: true,
  },
})