use `count` and execute SQL count(*) return value type is different
See original GitHub issueBug description
After enabling improvedQueryRaw, the database can correctly return the bigint type, but using prisma.model.count() is still the number type. Is this a bug?
How to reproduce
const [{ count }] = await Database.$queryRawUnsafe(`select count(*) from "user"`) as { count: any }[]
console.log(typeof count)
const count2 = await Database.user.count()
console.log(typeof count2)
out: bigint number
Expected behavior
Either unify as number or unify as bigint
Prisma information
Environment & setup
- OS: Windows
- Database: PostgreSQL
- Node.js version: 17.1
Prisma Version
prisma : 3.15.0
@prisma/client : 3.15.0
Current platform : windows
Query Engine (Node-API) : libquery-engine b9297dc3a59307060c1c39d7e4f5765066f38372 (at node_modules\.pnpm\@prisma+engines@3.15.0-29.b9297dc3a59307060c1c39d7e4f5765066f38372\node_modules\@prisma\engines\query_engine-windows.dll.node)
Migration Engine : migration-engine-cli b9297dc3a59307060c1c39d7e4f5765066f38372 (at node_modules\.pnpm\@prisma+engines@3.15.0-29.b9297dc3a59307060c1c39d7e4f5765066f38372\node_modules\@prisma\engines\migration-engine-windows.exe)
Introspection Engine : introspection-core b9297dc3a59307060c1c39d7e4f5765066f38372 (at node_modules\.pnpm\@prisma+engines@3.15.0-29.b9297dc3a59307060c1c39d7e4f5765066f38372\node_modules\@prisma\engines\introspection-engine-windows.exe)
Format Binary : prisma-fmt b9297dc3a59307060c1c39d7e4f5765066f38372 (at node_modules\.pnpm\@prisma+engines@3.15.0-29.b9297dc3a59307060c1c39d7e4f5765066f38372\node_modules\@prisma\engines\prisma-fmt-windows.exe)
Default Engines Hash : b9297dc3a59307060c1c39d7e4f5765066f38372
Studio : 0.462.0
Preview Features : interactiveTransactions, fullTextSearch, improvedQueryRaw
Issue Analytics
- State:
- Created a year ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Overview of SQL COUNT and COUNT_BIG in SQL Server
To get a number of rows, use COUNT (*). In this way, it returns all the items that include NON –NULL, NULL and...
Read more >What is the Difference Between COUNT(*), COUNT(1 ...
The COUNT(*) function counts the total rows in the table, including the NULL values. The semantics for COUNT(1) differ slightly; we'll discuss ...
Read more >Explaining COUNT return value when used without group
COUNT returns the number of items found in a group and by default it includes NULL values and duplicates. COUNT(ALL expression) evaluates ...
Read more >SQL: COUNT Function - TechOnTheNet
The SQL COUNT function is used to count the number of rows returned in a SELECT statement. Syntax. The syntax for the COUNT...
Read more >SQL Count(*) return type? | Ars OpenForum - Ars Technica
<P>*<P>Specifies that all rows should be counted to return the total number of rows in a table. COUNT(*) takes no parameters and cannot...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Although it is somewhat confusing, I don’t believe it’s a bug.
The
count()
method always returns a JavaScript number — that is a part of the API contract. It hasn’t been changed because there was no reason to change it intentionally, and doing that would have been a big breaking change.As for the
$queryRaw
, there was a bug where some values were converted to wrong type and information was lost. Now (first under theimprovedQueryRaw
feature flag and then in GA since Prisma 4.0.0) we try to always return the actual data type from the database, which fixed a big cluster of issues. Unfortunately this means that in some cases the results might look unintuitive since the actual data type is quite different from what might have been expected. We can’t (and must not) change those types because we don’t know the meaning of the raw query, so we just return data as they are. In this case, the database returns an int64, which is mapped to a bigint in JavaScript. You could get a different result depending on the database or its configuration.I’m going to go ahead and close this issue. Please let us know if you think this was a mistake!
Does that not also meant that
count()
is broken for tables with more entries than MAXINT? That would deserve another feature request or improvement issue then to be fixed in some way, right?