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.

Decimals lose precision (postgres)

See original GitHub issue

Bug description

The decimals sent to Postgres by the query engine lose precision

How to reproduce

run this script:

import { PrismaClient } from '@prisma/client'
import { Decimal } from '@prisma/client/runtime'
const prisma = new PrismaClient()

const run = async () => {
  const value = new Decimal('600.780000000012345678912345678912')
  console.log('before', { foo: value })
  const foo = await prisma.foo.create({
    data: {
      amountDecimal: value,
      amountString: value.toString(),
    },
  })
  console.log('after', { foo })
}
run()

Expected behaviour

I expect amountDecimal: 600.7800000006123456789 not 600.7800000006123,

Prisma information

model Foo {
  id                 String      @id @default(uuid())
  amountDecimal      Decimal  @db.Decimal(65, 30)
  amountString       String
}

SQL Table generated:

CREATE TABLE "Foo" (
    "id" TEXT NOT NULL,
    "amountDecimal" DECIMAL(65,30) NOT NULL,
    "amountString" TEXT NOT NULL,

    PRIMARY KEY ("id")
);

Environment & setup

prisma               : 2.21.2
@prisma/client       : 2.21.2
Current platform     : darwin
Query Engine         : query-engine e421996c87d5f3c8f7eeadd502d4ad402c89464d (at node_modules/@prisma/engines/query-engine-darwin)
Migration Engine     : migration-engine-cli e421996c87d5f3c8f7eeadd502d4ad402c89464d (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core e421996c87d5f3c8f7eeadd502d4ad402c89464d (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary        : prisma-fmt e421996c87d5f3c8f7eeadd502d4ad402c89464d (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash : e421996c87d5f3c8f7eeadd502d4ad402c89464d
Studio               : 0.371.0

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
matthewmuellercommented, May 19, 2021

We’ll be tackling this in #6902

0reactions
janpiocommented, Sep 9, 2022

For internal bookkeeping: This was considered a duplicate of https://github.com/prisma/prisma/issues/5925

Read more comments on GitHub >

github_iconTop Results From Across the Web

PostgreSQL real type losing integer precision when printed ...
The problem seems to originate from the way PHP returns results. The values are not returned as the corresponding data type, ...
Read more >
decimal lose precision digits to postgres · Issue #8160 - GitHub
MySQL DECIMAL(X, 5) returns incorrect precision. #13156 ; Decimals lose precision (postgres) #6852 ; [PostgreSQL] Cannot create or update row with large Decimal ......
Read more >
How to prevent PostgreSQL from automatically rounding ...
In this case, I have lost precision. I entered five decimal places, but Postgres automatically rounded down to four.
Read more >
Documentation: 9.3: Numeric Types - PostgreSQL
Numeric types consist of two-, four-, and eight-byte integers, four- and eight-byte floating-point numbers, and selectable-precision decimals.
Read more >
PostgreSQL Data Types - Numeric, Text, and More - Prisma
Specify precision followed by scale when configuring a column using both controls. PostgreSQL will round the decimal component of any input to the...
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