Hi there, I'm wondering what the best way to make ...
# orm-help
t
Hi there, I'm wondering what the best way to make a friendship system is? My current model looks like this
Copy code
model User {
  id      String  @id
  username String  @unique
}
And basically I want to tie together 2 users. Similar to just about every other app with a friends system, each user can have multiple friendships, but each friendship can only include 2 users. What is the best way to do this in Prisma with MySQL, in a way that I can easily list all friends of a given user? Thanks!
✅ 1
and if each friend can have many friends and be one of many friends you want the many-to-many section: https://www.prisma.io/docs/concepts/components/prisma-schema/relations/self-relations#many-to-many-self-relations
t
@jdkdev The example in there is
Copy code
model User {
  id         Int     @id @default(autoincrement())
  name       String?
  followedBy User[]  @relation("UserFollows")
  following  User[]  @relation("UserFollows")
}
But you can follow someone and them not be following you back, but with friends you can't do that. How would I adapt that to friends, where its a 2-way thing? Would I need 2 fields for friends, or just the one field that says
friends User[]
? Thanks so much for helping.
j
I have plenty of friends who don't call me back
just kidding
yeah I think it would be that but might need some relation() part with it
where you explicitly say it's the user.id field for connecting
if that doesn't seem to work, let me know.. @Thomas Yankue
the schema should yell at you pretty quickly if you are missing something
t
@jdkdev I decided to go down the route of having a friendship table too, since I'd like to store the date they've been friends since. The only solution I could find that works is
Copy code
model User {
  id      String  @id
  username String  @unique
  initiator  Friendship[] @relation("initiator")
  recipient  Friendship[] @relation("recipient")

  @@unique([id, username])
}

model Friendship {
  since DateTime

  initiator User @relation("initiator", fields: [initiatorID, initiatorName], references: [id, username])
  initiatorName String
  initiatorID String

  recipient User @relation("recipient", fields: [recipientID, recipientName], references: [id, username])
  recipientName String
  recipientID String

  @@id([initiatorID, recipientID])
}
It's not the most elegant solution, but using an OR i can easily find all the data I want (when it lists friends it'll only say the username, unless you specifically click on that user, in which case we can afford to make another DB query). I tried just having
friends Friendship[]
on the user, and just
member1 User
and
member2 User
on the friendship, but that didn't work (didn't really expect it to).This solution isn't the best, but to me it feels nicer than having
User[]
on the friendship model, and it provides all the data I need in just one query.
Thank you very much for the help
j
okay, yeah, the more I thought about it, I ended up thinking you were going to need something like the the Friendship Table