Is it possible to use a where clause with a nested...
# orm-help
c
Is it possible to use a where clause with a nested relation count? For example, select the users who have more than 500 posts?
r
@Copium Dealer 👋 Currently not, but it would be great if you could add a 👍 to this request so that we can look into this 🙂
👍 1
c
Wow that's so lame...
I'm finding myself using SQL more than prisma, even though I'm supposed to be using Prisma...
r
Could you elaborate on the use cases where you have had to use raw queries?
I want to do this, but also add the 500 minimum requirement also. Another thing I noticed is that Prisma Studio doesn't respect middleware. I understand why, but I think it's very strange.
r
It’s similar to filtering on aggregates.
c
I haven't written the SQL out yet, it will be quite tough as I havent written SQL in a long time. It's just inconvenient that most of the features you'd use in a practical setting dont exist
Obviously not your fault, I know these things take time.
🙌 1
r
The one with the Studio should be something that we would be working on as that’s quite a normal request.
c
Just ranting. Docs feel incomplete / not beginner friendly too. I can drop things that I think are missing in the Slack if that works. I don't usually F with github forums
r
Which part of the docs do you think are incomplete and not beginner friendly? People usually have the opposite opinion about our docs.
c
I guess because they are preview features. But stuff about counting relations, ordering by relations etc.
The previous thread I linked. Is that possible in one request in Prisma?
r
Yeah it’s the same request.
I guess because they are preview features. But stuff about counting relations, ordering by relations etc.
Is this regarding the docs?
âś… 1
c
"It's the same request" Sorry I don't understand
Also, I tried this
const results = await prisma.token.findMany({
select: { id: true, name: true, symbol: true, telegram: true, votes: { where: { createdAt: { gt: subDays(new Date(), 1) } } }, _count: { select: { votes: true } }, }, orderBy: { votes: { count: "desc" } }, });
But the count is just returned (not the count of the reduced
vote
set). I'm not sure if this is a bug, or intentional.
"Is this regarding the docs?" - Looking back on it, it doesn't look too bad. But I think it's the incompleteness that makes it more confusing (Like the code snippet I just sent).
r
“It’s the same request” Sorry I don’t understand
Basically you would want filtering on aggregates i.e. in this case the count of the votes should match the filters in the
where
clause.
c
Right. Do you mind providing an example? If that's no trouble for you
r
Like the code snippet I just sent)
The one above? We have mostly everything covered in the docs related to preview features. If somethings not working, it’s most likely that it isn’t supported. Also TypeScript and
ctrl+space
can help you with the parameters that the API accepts 🙂
Right. Do you mind providing an example? If that’s no trouble for you
Yeah this is similar to the one you posted in the main question. Filtering on aggregates is not yet supported so the above query will not give you the required result as the count wouldn’t take the
where
condition into account.
👍 1
c
So in the scenario posted in the previous thread, is the best course of action an SQL statement?
r
Yes for now until the request is implemented natively.
c
The best solution seems to be to set a boolean on the
token
object. Set this to true when votes reach 500. The only way I can see in the docs is through a middleware, or a cron job. Are there any better solutions that are more robust?