Where is the best place to get help / advice for d...
# orm-help
d
Where is the best place to get help / advice for developing with Prisma? I have a question regarding how to set up my model in my schema.
m
Hey Douglas! šŸ‘‹šŸ¼ You can ask here! I’d love to help šŸ˜„
d
Cool, thanks! I was wondering how I would create a model to solve a problem where a User can save a list of other Users in addition to a related number to them. The problem is basically a following / followers structure, with an abstract number of "how many times have I clicked on this person". I think I have managed to solve it using this structure, in this GitHub issue: https://github.com/prisma/prisma/issues/2217
Copy code
model Follows {
  follower    User @relation("follower", fields: [followerId], references: [id])
  followerId  Int
  following   User @relation("following", fields: [followingId], references: [id])
  followingId Int

  @@id([followerId, followingId])
}

model User {
  id        Int       @default(autoincrement()) @id
  createdAt DateTime  @default(now())
  followers Follows[] @relation("follower")
  following Follows[] @relation("following")
}
But where I have also added a field for
Copy code
model Follows {
   // how many times the person has been clicked
   clicked Int
  ...
So in the end, I think this solves things? I guess the only thing I might want to change is that I don't want to know if someone has any followers, simply how many times they have clicked a specific user.
Wait, I just realised I need the field to be different for how many times Person A has clicked Person B and how many times Person B has clicked Person A.
This might make it more complicated...
I'm not sure if it makes things more complicated and this might be a difficult problem for you as well šŸ˜…. I guess a further metaphor / abstraction would be that this is Tinder. You would like to save if Person A has swiped yes on Person B, or if they have swiped no (if you would use a boolean rather than a number).
c
Just add a clickCount field to your Follows table. Problem solved šŸ™‚.
plus one +1 3
d
Thanks!
g
I am adding followers / following to my schema too and went for a self relation:
Copy code
model User {
  id        String  @id @default(cuid())
  following User[]  @relation(name: "UserFollows")
  followers User[]  @relation(name: "UserFollows")
}
is this a bad idea? Would it be better to have a separate model for this and store (and maintain) the count on
User
(for perf reasons?)
šŸ‘€ 1
cc @Mahmoud
by the way can you recommend somebody who could review a small schema? A db architect or anybody who has enough experience with modeling data
d
Hey @Giuseppe šŸ‘‹, That is a known bug (https://github.com/prisma/prisma/issues/7299 https://github.com/prisma/prisma/issues/7893) and will be resolved in the near future. In the meanwhile, I would suggest using the explicit many-to-many notation as suggested above:
Copy code
model Follows {
  follower   User @relation("follower", fields: [followerId], references: [id], onDelete: Cascade)
  followerId Int
  followee   User @relation("followee", fields: [followeeId], references: [id], onDelete: Cascade)
  followeeId Int

  @@id([followerId, followeeId])
}

model User {
  id        Int       @id @default(autoincrement())
  name      String
  createdAt DateTime  @default(now())
  followers Follows[] @relation("follower")
  followees Follows[] @relation("followee")
}
I just tested and the selecting relation counts works with this approach.
ā¤ļø 2
g
awesome thanks a lot for the help!!!
šŸ™ 1