Given a user id
, I want to find the followers that the user follows (i.e. follow each other)
My Prisma model looks like the following:
model User {
id Int @id @default(autoincrement())
name String?
followedBy Follows[] @relation("following")
following Follows[] @relation("follower")
}
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])
}
I am also interested in counting them – this could be done in a separate query since the former might require pagination at some point.
Thank you in advance for your help.
2
Answers
Probably a raw query will do
and counting:
sadly it seems that Prisma's
queryRaw
doesn't supportAS
so I had to usequeryRawUnsafe
and need to sanitize the input (not done in the code above).To find the mutual followers you can do something like this:
This requires some post processing of records to find the mutual follows of each other.
Here’s the output: