I use nestjs
and postgresql
with prisma
. I have 2 tables in relation, I want to create a where clause in order to fetch the records if count of the records in the second table is less than -let's say- 3. More details;
Here is my schema
model User {
id String @id
someOtherFields String
outgoingPlayMateRequest PlayMateRequest[] @relation("userId")
incomingPlayMateRequest PlayMateRequest[] @relation("targetId")
}
model PlayMateRequest {
id Int @id
requestingUser User @relation(name: "userId", fields: [requestingUserId], references: [id], onDelete: Cascade)
targetUser User @relation(name: "targetId", fields: [targetUserId], references: [id], onDelete: Cascade)
requestingUserId String
targetUserId String
someOtherFields String
response String //accept-reject-block
}
and here is my code with where clause (I am simplfying it by removing unrelevant parts)
const userId = 'testUser';
return await this.prismaService.user.findMany({
where: {
NOT: {
id: userId //don't fetch user him/herself
},
lang: 'EN',
}
});
The condition I want to add here in english is;
Don't select users with incomingPlayMateRequest relation, if there are 3 records in PlayMateRequest table with
response = reject
ANDrequestingUser = userId
But I couldn't find anyway to use count
as a condition in where. As I see I can only get the relations count. How can I do this with prisma
?