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.

use `count` and execute SQL count(*) return value type is different

See original GitHub issue

Bug 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:closed
  • Created a year ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
aqrlncommented, Sep 2, 2022

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 the improvedQueryRaw 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!

0reactions
janpiocommented, Sep 2, 2022

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?

Read more comments on GitHub >

github_iconTop 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 >

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