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.

$executeRaw throws PostgreSQL ERROR: invalid scale in external "numeric" value or inserts 0.0

See original GitHub issue

Bug description

We are using $executeRaw to insert data into a table. If a decimal value is passed in the record will get inserted. If a whole number (1234, 1234.0) is passed in, the record will be inserted but the decimal value will be 0.0. If a larger whole number (12345, 12345.0) is passed in, Postgres will throw an error: ERROR: invalid scale in external “numeric” value

How to reproduce

CREATE TABLE IF NOT EXISTS "someTable" (
  "accountId" uuid PRIMARY KEY,
  "originalAmount" decimal(12,2),
  "notes" varchar(500)
);
model someTable {
  accountId      String  @id
  originalAmount Float?
  notes          String?
}
async runRawQuery(value: number, accountId: string, notes: string) {
    await this.client.$executeRaw`
    INSERT INTO "someTable" ("originalAmount","accountId","notes")
    VALUES (${value}, ${accountId}, ${notes});
    `
  }

Using runRawQuery(1234.5, '032c9baa-0616-4361-8a40-7a808e5180f3', 'some notes') will create the record with the correct values.

Using runRawQuery(1234.0, '032c9baa-0616-4361-8a40-7a808e5180f3', 'some notes') will create the record but originalAmount will be 0.00

Using runRawQuery(12345.0, '032c9baa-0616-4361-8a40-7a808e5180f3', 'some notes') will cause Postgres to throw an error: ERROR: invalid scale in external “numeric” value

Expected behavior

I expect the record to get created with the correct values.

Environment & setup

  • OS: Windows 10
  • Database: PostgreSQL 12.2
  • Node.js version: 12.16.1
  • Prisma version: found using 2.10.2, also duplicated on 2.13.1
Prisma CLI version: prisma/1.34.10 (windows-x64) node-v12.16.1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
pimeyscommented, Feb 10, 2021

JavaScript has a great feature, when we write 12345.0, it actually just decides this is not a floating point, but an integer, and sends down 12345. Then, we have a numeric field so we instead of writing a decimal, write the byte representation of the integer, which as a decimal is just zero.

We can of course fix this, patch incoming in 2.17.0.

0reactions
pantharshit00commented, Jan 13, 2021

I can reproduce the loss of value but I can’t reproduce invalid scale in "numeric" value error. I used 2.14 for reproduction.

Marking this as a confirmed bug though and we need to fix the JS value coercion here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

invalid sign in external "numeric" value error from Npgsql ...
I caused this problem when using MapBigInt, not realizing the column was accidentally numeric(18). Changing the column to bigint fixed my ...
Read more >
Re: BUG #12053: Strange behavior for numeric types with ...
The documentation for numeric says: Specifying: NUMERIC. without any precision or scale creates a column in which numeric values of
Read more >
Prisma 2.17.0 Release - GitClear
Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, ... when using executeRaw to insert number values to a numeric type in ...
Read more >
ORMLite Documentation
A Java String persisted as an array of bytes ( byte[] ) with the SQL type VARBINARY . Many databases are Unicode compliant...
Read more >
mysql add decimal point Code Example - Code Grepper
column_name DECIMAL(precision, scale) -- DECIMAL stores exact numeric values with a fixed decimal point. -- The precision is the total number of digits ......
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