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.

BigInt becomes Number if queried with `$queryRaw`

See original GitHub issue

Bug description

When I try to $queryRaw() a BigInt column, it becomes Number, resulting in loss of precision for greater number than Number.MAX_SAFE_INTEGER.

// This user has ID of `BigInt(1)` with schema written below.
await prisma.user.create({
  data: { name: 'dummy' },
})

const user1 = (await prisma.user.findFirst()) as User
// This output `true`, as expected
console.log({ user: user1, isSame: user1.id === BigInt(1) })

const [user2] = await prisma.$queryRaw<User[]>`SELECT id, name FROM "User"`
// However, this output `false`, as `typeof user2.id === 'number'`
console.log({ user: user2, isSame: user2.id === BigInt(1) })

How to reproduce

I created a minimal reproduction repository of this issue.

$ git clone https://github.com/yukidaruma/prisma-bigint-rawquery
$ cd prisma-bigint-rawquery
$ npm i
$ docker-compose up -d
$ npx prisma migrate dev --name bigint_rawquery_test
$ npm run dev

> dev
> ts-node ./script.ts

{ user: { id: 1n, name: 'dummy' }, isSame: true } // Using `findFirst()` method
{ user: { id: 1, name: 'dummy' }, isSame: false } // Using `$queryRaw()` method 

Expected behavior

I expect $queryRaw() to handle BigInt column properly, returning BigInt value instead of Number value.

{ user: { id: 1n, name: 'dummy' }, isSame: true } // Using `findFirst()` method
{ user: { id: 1n, name: 'dummy' }, isSame: true } // Using `$queryRaw()` method

Prisma information

// Can be found at: https://github.com/yukidaruma/prisma-bigint-rawquery/blob/main/prisma/schema.prisma

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
}

model User {
  id   BigInt @id @default(autoincrement())
  name String
}

Environment & setup

  • OS: Windows
  • Database: PostgreSQL 13.3 (with postgres:13.3-alpine Docker image)
  • Node.js version: v16.1.0

Prisma Version

Environment variables loaded from prisma\.env
prisma               : 2.23.0
@prisma/client       : 2.23.0
Current platform     : windows
Query Engine         : query-engine adf5e8cba3daf12d456d911d72b6e9418681b28b (at node_modules\@prisma\engines\query-engine-windows.exe)
Migration Engine     : migration-engine-cli adf5e8cba3daf12d456d911d72b6e9418681b28b (at node_modules\@prisma\engines\migration-engine-windows.exe)
Introspection Engine : introspection-core adf5e8cba3daf12d456d911d72b6e9418681b28b (at node_modules\@prisma\engines\introspection-engine-windows.exe)
Format Binary        : prisma-fmt adf5e8cba3daf12d456d911d72b6e9418681b28b (at node_modules\@prisma\engines\prisma-fmt-windows.exe)
Default Engines Hash : adf5e8cba3daf12d456d911d72b6e9418681b28b
Studio               : 0.393.0

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
gersmanncommented, Nov 3, 2021

Too bad I had to find out about this after migrating quite some logic to Prisma in dev. Any chance that there will be a fix for this soon? This is critical.

0reactions
Weakkycommented, May 5, 2022

Hey,

This issue was fixed by https://github.com/prisma/prisma-engines/pull/2847. It will be available in the next release under the improvedQueryRaw feature flag.

Beware that enabling improvedQueryRaw is a breaking change. Release notes will contain information as to how to upgrade (so will the documentation).

Thanks for reporting 🙏

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prisma 3.14.0 Release - GitClear
fix(client): integrate queryRaw improvements (#13066) ... BigInt becomes Number if queried with $queryRaw · Weird behavior of raw query ...
Read more >
ORMLite Documentation
If this is set to true then, when the object is queried, a separate database call will be made to load of the...
Read more >
4. Querying Delimited Data - Learning Apache Drill [Book]
We are defining “simple data” as data contained in a delimited file such as a spreadsheet or comma-separated values (CSV) file, from a...
Read more >
Getting wrong result when using bigint in sql query
try converting to numeric instead of bigint: DECLARE @temp float set @temp = 2412880.28 SELECT CONVERT(varchar,(CONVERT(numeric(27,0) ...
Read more >
bigrquery source: R/bq-download.R - Rdrr.io
@param page_size The number of rows requested per chunk. ... @inheritParams api-job #' @param bigint The R type that BigQuery's 64-bit integer types...
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