Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.raw errors on date params w/ Postgres #2533

Closed
cap10morgan opened this issue May 22, 2020 · 5 comments · Fixed by #2823
Closed

.raw errors on date params w/ Postgres #2533

cap10morgan opened this issue May 22, 2020 · 5 comments · Fixed by #2823
Assignees
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. tech/engines Issue for tech Engines. tech/typescript Issue for tech TypeScript. topic: raw $queryRaw(Unsafe) and $executeRaw(Unsafe): https://www.prisma.io/docs/concepts/components/prisma-cli
Milestone

Comments

@cap10morgan
Copy link

cap10morgan commented May 22, 2020

Bug description

When running a raw SQL query with prisma.raw and template literal params, I get the error below when querying on timestamp columns:

error: PrismaClientKnownRequestError: Raw query failed. Code: `22P03`. Message: `db error: ERROR: incorrect binary data format in bind parameter 2`
  engine       at NodeEngine.graphQLToJSError (/Users/wmorgan/dev/safe-office/api/node_modules/@prisma/engine-core/dist/NodeEngine.js:590:1)
  engine       at /Users/wmorgan/dev/safe-office/api/node_modules/@prisma/engine-core/dist/NodeEngine.js:498:1
  engine       at process.result (internal/process/task_queues.js:97:5)
  engine       at PrismaClientFetcher.request (/Users/wmorgan/dev/safe-office/api/node_modules/@prisma/client/src/runtime/getPrismaClient.ts:628:13) {
  engine     code: 'P2010',
  engine     meta: {
  engine       code: '22P03',
  engine       message: 'db error: ERROR: incorrect binary data format in bind parameter 2'
  engine     }
  engine   }
  engine }

How to reproduce

  1. Create a DB table in Postgres with a timestamp without time zone field.
  2. Query it via prisma.raw like this: prisma.raw`select * from table where tzfield=${dateValue}`
    1. For dateValue I tested with both ISO-formatted date strings and JS Date objects.

Expected behavior

The query works and returns the same data as a query not involving timestamp fields.

Environment & setup

  • OS: macOS
  • Database: PostgreSQL
  • Prisma version: 2.0.0-beta.5
  • Node.js version: v12.16.3
@janpio janpio added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. topic: raw $queryRaw(Unsafe) and $executeRaw(Unsafe): https://www.prisma.io/docs/concepts/components/prisma-cli labels May 22, 2020
@pantharshit00
Copy link
Contributor

I can confirm the issue. My guess is that we are implicitly converting every date into ISO format in the client code.

Reproduction:

  1. Bootstrap postgres database with following SQL:
DROP TABLE IF EXISTS "public"."example";
-- Sequence and defined type
CREATE SEQUENCE IF NOT EXISTS example_id_seq;
-- Table Definition
CREATE TABLE "public"."example" (
    "id" int4 NOT NULL DEFAULT nextval('example_id_seq'::regclass),
    "time" timestamp,
    PRIMARY KEY ("id")
);
INSERT INTO "public"."example" ("id", "time") VALUES
('1', '2020-05-29 16:08:54.028741');
  1. Introspect the database and generate the client
  2. Run the following code:
import { PrismaClient } from "@prisma/client";

async function main() {
  const client = new PrismaClient();

  const date = "2020-05-29T16:08:54.028741+00:00";
  console.log(date);

  const data = await client.raw`select * from example where time = ${date}`;
  console.log(data);
  client.disconnect();
}

main();

@pantharshit00 pantharshit00 added bug/2-confirmed Bug has been reproduced and confirmed. process/candidate and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels May 29, 2020
@janpio janpio added tech/engines Issue for tech Engines. tech/typescript Issue for tech TypeScript. labels Jun 10, 2020
@janpio janpio added this to the 2.1.0 milestone Jun 10, 2020
@pantharshit00 pantharshit00 removed their assignment Jun 10, 2020
@pimeys pimeys assigned pimeys and unassigned do4gr Jun 19, 2020
@pimeys
Copy link
Contributor

pimeys commented Jun 22, 2020

The client fix is coming later today. From now on when wanting to use dates in raw queries, they should be provided as JavaScript Date objects for them to work.

@timsuchanek
Copy link
Contributor

And the client part is also merged.
In order to make Date work, you now need to explicitly provide a Date instance in JavaScript, otherwise, we can't reliably and fast coerce the type.

@johhansantana
Copy link

This should be explained somewhere in the docs. I didn't see anything relating this there.

@janpio
Copy link
Member

janpio commented Sep 1, 2020

cc @mhwelander

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. tech/engines Issue for tech Engines. tech/typescript Issue for tech TypeScript. topic: raw $queryRaw(Unsafe) and $executeRaw(Unsafe): https://www.prisma.io/docs/concepts/components/prisma-cli
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants