$executeRaw throws PostgreSQL ERROR: invalid scale in external "numeric" value or inserts 0.0
See original GitHub issueBug 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:
- Created 3 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top GitHub Comments
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 down12345
. 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.
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.