#Modeling users friendships with prisma ?

1 messages · Page 1 of 1 (latest)

lofty umbra
#

I am wondering how to model friendships between users in my models.
This only thing I could come up with is a followed/follower model but I am thinking there must be a better way

model User {
  name        String       @id
  password    String
  discussions Discussion[]

  friendList   User[] @relation("friendList")
  friendOfList User[] @relation("friendList")

  incomingFriendInvitation  User[] @relation("friendInvitation")
  outcomingFriendInvitation User[] @relation("friendInvitation")

  blockedUserList   User[] @relation("blockedUser")
  blockedByUserList User[] @relation("blockedUser")
}

model Discussion {
  id       Int       @id @default(autoincrement())
  title    String?
  users    User[]
  messages Message[]
}

I was thinking maybe this could work like that :
If I follow you but you don't follow me it's like I've sent you an invitation but you have not yet accepted it. A friendship being a mutual followance. But I am thinking this might become non trivial to retrieve.

#

Modeling users friendships with prisma ?

short cloud
#

Use a join table jnstead of array columns.

#

User +--+ Friendship +--+ User

#

The friendship table will have a primary key (UserId, FriendId). So a mutual friendship will be denoted by 2 records in the Friendship table

lofty umbra
#

I have tried to read about join tables but I can't say I understand them beyond the basic principle of it.
They're like a database made out of other db values right ? Zipping data together from other tables ?
So my friendship table would zip two users together

I suspect I need prisma'as@@id() right ?

#

Sth like ?

model Friendship {
    friend1        User @relation("friend1", references: [name])
    friend2        User @relation("friend2",  references: [name])
    @@id([friend1, friend2])
}
#

I believe relations can't be ids though 😓

icy spire
#
model User2 {
  id String @id @default(cuid())
  email String @unique
  friendshipsInitiated Friendship[] @relation("friendshipInitiator")
  friendshipsReceived Friendship[] @relation("friendshipRecipient")
}

model Friendship {
    initiatorId String
    initiator        User2 @relation("friendshipInitiator", references: [id], fields: [initiatorId])
    recipientId String
    recipient        User2 @relation("friendshipRecipient",  references: [id], fields: [recipientId])
    @@id([initiatorId, recipientId])
}

Something like this.

Although, I'd prefer Friendship to have own id for some reason:

model Friendship {
    id String @default(cuid())
    initiatorId String
    initiator        User2 @relation("friendshipInitiator", references: [id], fields: [initiatorId])
    recipientId String
    recipient        User2 @relation("friendshipRecipient",  references: [id], fields: [recipientId])
    @@unique([initiatorId, recipientId])
}

Also, user2 should be just user. I was just hacking it in existing schema and was lazy.