1

I am struggling to understand how to build multiple relations between two models. Take the following:

model User {
  id             Int       @default(autoincrement()) @id
  goalBoard      GoalBoard[]
  ...
}

model GoalBoard {
  id          Int         @default(autoincrement()) @id
  goalOwner   User        @relation(fields: [goalOwnerId], references: [id])
  goalOwnerId Int
  invitee     User[]      @relation(name: "invitee") //?
  invited     User[]      @relation(name: "invited") //?
  ...
  @@unique([goalOwnerId, active])
}

The 'invitee' & 'invited' fields will have multiple users within them. I am not clear on the need / requirement for the adding a name to the @relation.

So, for understanding sake, what is the purpose behind naming the @relation. And secondly, how would I build the relation as per my requirement above.

Thank you.

Kayote
  • 14,579
  • 25
  • 85
  • 144

1 Answers1

1

Here are the right and clear way to do your multiple many to many relations

enter image description here

model User {
  id               Int         @default(autoincrement()) @id
  goalBoardInvitee GoalBoard[] @relation(name: "UserGoalBoardInvitee")
  goalBoardInvited GoalBoard[] @relation(name: "UserGoalBoardInvited")
  ...
}

model GoalBoard {
  id          Int         @default(autoincrement()) @id
  goalOwner   User        @relation(fields: [goalOwnerId], references: [id])
  goalOwnerId Int
  invitee     User[]      @relation(name: "UserGoalBoardInvitee") //?
  invited     User[]      @relation(name: "UserGoalBoardInvited") //?
  ...
  @@unique([goalOwnerId, active])
}

To understand why we do this, please read the Prisma relations docs

Ahmed Elywa
  • 439
  • 3
  • 7
  • Thank you so much and as always Ahmed. Finally, this makes sense to me. Cheers. – Kayote Oct 10 '20 at 19:03
  • 1
    Any reason makes you not mark my Answer the correct answer? – Ahmed Elywa Oct 10 '20 at 22:56
  • I wanted to implement it to make sure I didnt have any further questions. Am going to implement and test the logic now. So hope to mark it soon. Thank you again for the help! – Kayote Oct 11 '20 at 06:40