drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[BUG]: PG Numeric inferred as string, but is numeric at runtime

Open olafkrawczyk opened this issue 1 year ago • 4 comments

What version of drizzle-orm are you using?

0.31.2

What version of drizzle-kit are you using?

0.22.7

Describe the Bug

Linked to: https://github.com/drizzle-team/drizzle-orm/issues/1042

export const  myTable = pgSchema('mySchema').table(
  'my_table',
  {
    id: uuid('id').notNull(),
    totalAmount: numeric('totalAmount', { precision: 78 }),
  },
export type MyTable = typeof myTable.$inferSelect;

When using numeric as above the inferred type is string, but at runtime we get number. Possibly it leads to loss of the precision.

Given totalAmount of

100000000000000000000000000000000000000000000000000000000000000000000000000001

select query returns

 1e+77

Expected behavior

Inferred type and runtime type should be the same

Environment & setup

No response

olafkrawczyk avatar Jul 24 '24 07:07 olafkrawczyk

I am having this same issue with 0.31.2

K-Mistele avatar Jul 24 '24 22:07 K-Mistele

I'm facing this issue as well 0.32.2

sayandcode avatar Aug 07 '24 12:08 sayandcode

Type casting workaround (don't use if the potential loss of precision is important)

price: numeric('price', { precision: 15, scale: 6 }).notNull()

becomes

price: numeric('price', { precision: 15, scale: 6 }).$type<number>().notNull()

magebase-dev avatar Aug 11 '24 07:08 magebase-dev

I'm facing this issue with 0.33.0, any plans to solve this? It is a pretty significant issue as it results in a loss of precision

Royleong31 avatar Sep 26 '24 15:09 Royleong31

Same with 0.33.0 and Postgres.js 3.4.4

KostarSf avatar Oct 04 '24 12:10 KostarSf

In my case, I have 2 tables. "users", and "balances". the "balances" has an "amount " column, and it is a "numberic(18, 8)" at pg. level.

If I am querying for the "balances", in different cases I get back a different data type for the "amount".

Let me describe the 2 case:

// In this case the data type is matching to the infered type. (The balance.amount is a string)
const balance = await this.drizzleService.db.query.balances.findFirst({
	where: eq(schema.balances.id, 1),
});

// In this case the data type is number, however at the ts. lvl it's inferred as a string (The userBalance.balance.amount is a number)
const userBalance = await this.drizzleService.db.query.users.findFirst({
	where: eq(schema.users.id, 'test-id'),
	with: { balance: true },
});
{
	"drizzle-kit": "^0.26.2",
	"drizzle-orm": "^0.35.1",
}

fabicsp avatar Oct 16 '24 11:10 fabicsp

Same issue with drizzle-orm v0.39.3 and pg v8.13.3. This is a real blocker as we are losing precision due to this.

claytonchew avatar Mar 18 '25 11:03 claytonchew

@claytonchew here is a workaround we use:

amount: numeric({ scale: 2, precision: 12 }).$type<number>().notNull(),

I think I got it from https://github.com/drizzle-team/drizzle-orm/issues/2681#issuecomment-2282660800

I don't think precision should be lost, this is just for the typings, the runtime value is not changed.

lirbank avatar Mar 19 '25 19:03 lirbank

This also works:

amount: numeric({ scale: 2, precision: 12, mode: "number" })

lirbank avatar May 21 '25 19:05 lirbank

Hey everyone!

I've created this message to send in a batch to all opened issues we have, just because there are a lot of them and I want to update all of you with our current work, why issues are not responded to, and the amount of work that has been done by our team over ~8 months.

I saw a lot of issues with suggestions on how to fix something while we were not responding – so thanks everyone. Also, thanks to everyone patiently waiting for a response from us and continuing to use Drizzle!

We currently have 4 major branches with a lot of work done. Each branch was handled by different devs and teams to make sure we could make all the changes in parallel.


First branch is drizzle-kit rewrite

All of the work can be found on the alternation-engine branch. Here is a PR with the work done: https://github.com/drizzle-team/drizzle-orm/pull/4439

As you can see, it has 167k added lines of code and 67k removed, which means we've completely rewritten the drizzle-kit alternation engine, the way we handle diffs for each dialect, together with expanding our test suite from 600 tests to ~9k test units for all different types of actions you can do with kit. More importantly, we changed the migration folder structure and made commutative migrations, so you won't face complex conflicts on migrations when working in a team.

What's left here:

  • We are finishing handling defaults for Postgres, the last being geometry (yes, we fixed the srid issue here as well).
  • We are finishing commutative migrations for all dialects.
  • We are finishing up the command, so the migration flow will be as simple as drizzle-kit up for you.

Where it brings us:

  • We are getting drizzle-kit into a new good shape where we can call it [email protected]!

Timeline:

  • We need ~2 weeks to finish all of the above and send this branch to beta for testing.

Second big branch is a complex one with several HUGE updates

  • Bringing Relational Queries v2 finally live. We've done a lot of work here to actually make it faster than RQBv1 and much better from a DX point of view. But in implementing it, we had to make another big rewrite, so we completely rewrote the drizzle-orm type system, which made it much simpler and improved type performance by ~21.4x:
(types instantiations for 3300 lines production drizzle schema + 990 lines relations)

TS v5.8.3: 728.8k -> 34.1k
TS v5.9.2: 553.7k -> 25.4k

You can read more about it here.

What's left here:

Where it brings us:

  • We are getting drizzle-orm into a new good shape where we can call it [email protected]!

Breaking changes:

  • We will have them, but we will have open channels for everyone building on top of drizzle types, so we can guide you through all the changes.

Third branch is adding support for CockroachDB and MSSQL dialects

Support for them is already in the alternation-engine branch and will be available together with the drizzle-kit rewrite.

Summary

All of the work we are doing is crucial and should be done sooner rather than later. We've received a lot of feedback and worked really hard to find the best strategies and decisions for API, DX, architecture, etc., so we can confidently mark it as v1 and be sure we can improve it and remain flexible for all the features you are asking for, while becoming even better for everyone building on top of the drizzle API as well.

We didn't want to stay with some legacy decisions and solutions we had, and instead wanted to shape Drizzle in a way that will be best looking ahead to 2025–2026 trends (v1 will get proper effect support, etc.).

We believe that all of the effort we've put in will boost Drizzle and benefit everyone using it.

Thanks everyone, as we said, we are here to stay for a long time to build a great tool together!

Timelines

We are hoping to get v1 for drizzle in beta this fall and same timeline for latest. Right after that we can go through all of the issues and PRs and resond everyone. v1 for drizzle should close ~70% of all the bug tickets we have, so on beta release we will start marking them as closed!

AndriiSherman avatar Aug 30 '25 18:08 AndriiSherman