question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prisma Client: `.count` does not work with where

See original GitHub issue

Bug description

When passing the where to prisma.relation.count, the parameters are not considered, as if they had not been passed

How to reproduce

The code

console.log('query without where')

const c = await prisma.user.count()

console.log(`response: ${c}\n\n`)

console.log('query with where id = 1')

const d = await prisma.user.count({
  where: {
    id: 1
  }
})

console.log(`response: ${d}`)

The result

query without where
prisma:info Starting a postgresql pool with 5 connections.
prisma:query SELECT COUNT(*) FROM (SELECT "public"."users"."id" FROM "public"."users" WHERE 1=1 OFFSET $1) AS "sub"
response: 24


query with where id = 1
prisma:query SELECT COUNT(*) FROM (SELECT "public"."users"."id" FROM "public"."users" WHERE 1=1 OFFSET $1) AS "sub"
response: 24

Expected behavior

No response

Prisma information

They are in “How to reproduce”

Environment & setup

  • OS: KDE neon 5.23
  • Database: PostgreSQL 13.3
  • Node.js version: v16.13.2

Prisma Version

prisma                  : 3.9.0
@prisma/client          : 3.9.0
Current platform        : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine bcc2ff906db47790ee902e7bbc76d7ffb1893009 (at node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine        : migration-engine-cli bcc2ff906db47790ee902e7bbc76d7ffb1893009 (at node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine    : introspection-core bcc2ff906db47790ee902e7bbc76d7ffb1893009 (at node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary           : prisma-fmt bcc2ff906db47790ee902e7bbc76d7ffb1893009 (at node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Default Engines Hash    : bcc2ff906db47790ee902e7bbc76d7ffb1893009
Studio                  : 0.457.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:11
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

19reactions
icflorescucommented, Feb 1, 2022

This is actually pretty bad, I wonder how it passed the tests and ended up being published 😐

17reactions
millspcommented, Feb 2, 2022

Sounds like a great opportunity to beef up tests

Definitely, this has clearly shown that some areas were under-tested. The next steps will be to build a more robust testing setup and we will start porting existing tests (reviewing and potentially upgrading) into this new folder. Work is starting this sprint.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Aggregation, grouping, and summarizing (Concepts) - Prisma
Aggregation, grouping, and summarizing. Prisma Client allows you to count records, aggregate number fields, and select distinct field values.
Read more >
Using _count in a select query with Prisma - sql - Stack Overflow
As far as I know it is not possible to use _count in a findMany query, so I tried to use select in...
Read more >
Getting Counts on Relations with Prisma Client - YouTube
New at Prisma 2.20 is the ability to get the counts of related fields. This is useful for cases where you'd like to...
Read more >
Prisma plugin for Pothos GraphQL
queryType({ fields: (t) => ({ // Define a field that issues an optimized prisma query me: t.prismaField({ type: 'User', resolve: async (query, root,...
Read more >
How to get counts on relations with Prisma client
This is imperative when we have a dashboard or using it on a dashboard where we can extract the number of posts or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found