Thomas Yankue
07/26/2022, 2:48 PMmodel 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!jdkdev
07/26/2022, 3:47 PMjdkdev
07/26/2022, 3:49 PMThomas Yankue
07/26/2022, 5:35 PMmodel 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.jdkdev
07/26/2022, 5:42 PMjdkdev
07/26/2022, 5:43 PMjdkdev
07/26/2022, 5:45 PMjdkdev
07/26/2022, 5:46 PMjdkdev
07/26/2022, 6:01 PMjdkdev
07/26/2022, 6:02 PMThomas Yankue
07/26/2022, 6:09 PMmodel 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.Thomas Yankue
07/26/2022, 6:09 PMjdkdev
07/26/2022, 6:14 PM