drizzle-team / drizzle-team/drizzle-orm
[BUG]: Precision loss of bigints when including relations in postgres
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
### Report hasn't been filed before.
- [x] I have verified that the bug I'm about to report hasn't been filed before.
### What version of `drizzle-orm` are you using?
0.45.2
### What version of `drizzle-kit` are you using?
0.31.10
### Other packages
pg@8.20.0
### Describe the Bug
The bug was filed before, but the author looks like has abandoned it: https://github.com/drizzle-team/drizzle-orm/issues/4894
It's the same thing, but now with a repro repo: https://github.com/Malien/drizzle-repro
```
❯ DATABASE_URL=postgresql://username:password@localhost/boohoo bun index.ts
expected
{
id: 1000000000000000001n,
posts: [
{
id: 1000000000000000002n,
title: "foo",
author: 1000000000000000001n,
}, {
id: 1000000000000000003n,
title: "bar",
author: 1000000000000000001n,
}
],
}
got
{
id: 1000000000000000001n,
posts: [
{
id: 1000000000000000000n,
title: "foo",
author: 1000000000000000000n,
}, {
id: 1000000000000000000n,
title: "bar",
author: 1000000000000000000n,
}
],
}
```
The original author was in fact incorrect, the constructed json string coming from the postgres correctly accounts for the precision. It's the JSON.parse somewhere down the line that is screwing up by parsing JSON numbers as js numbers (ie. the thing it was designed to do). It can be amended to parse known columns (array indices) as bigints tho (with `context.source` parameter of replacer for the JSON.parse).
Here's the exact drizzle query fired pasted into psql:
```
boohoo=# select "users"."id", "users_posts"."data" as "posts" from "users" "users" left join lateral (select coalesce(json_agg(json_build_array("users_posts"."id", "users_posts"."title", "users_posts"."author")), '[]'::json) as "data" from "posts" "users_posts" where "users_posts"."author" = "users"."id") "users_posts" on true limit 1;
id | posts
---------------------+--------------------------------------------------------------------------------------------------------
1000000000000000001 | [[1000000000000000002, "foo", 1000000000000000001], [1000000000000000003, "bar", 1000000000000000001]]
```
---
If you'd preferred this to instead be amended to that issue for tracking sake, sure — let's close this one and amend that one.
Just thought that in the sea of 1.3K open issues, no-one will bat an eye to the amendment at one marked as "[bug/cant-reproduce](https://github.com/drizzle-team/drizzle-orm/issues?q=state%3Aopen%20label%3A%22bug%2Fcant-reproduce%22)"
Contributor guide
Research direction
Start by running the linked reproduction repo's index.ts against PostgreSQL and compare its result with the SQL output shown in the issue. Trace the relation result through the JSON parsing step; done means bigint IDs retain their exact values in the nested posts result.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- postgresql, typescript
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100