Skip to content

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

@kennytraction

Description

@kennytraction

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

Metadata

Metadata

Assignees

Labels

bug/2-confirmedBug has been reproduced and confirmed.kind/bugA reported bug.topic: floating point typesTopic related to floating point types and precision losstopic: raw$queryRaw(Unsafe) and $executeRaw(Unsafe): https://www.prisma.io/docs/concepts/components/prisma-cli

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions