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.

Postgres parameterized interval is passed incorrectly in raw query

See original GitHub issue

Bug description

Consider the following query:

  const endDate = new Date('2020-12-15T06:00:00');
  const startDate = new Date('2020-12-20T06:00:00Z');
  const interval = '1 day';

  const result = await ctx.prisma.$queryRaw`
    SELECT
      start_ts AS timestamp,
      COUNT(*) AS value
    FROM
      generate_series(${startDate}::TIMESTAMP, ${endDate}::TIMESTAMP, ${interval}::INTERVAL) AS start_ts
    LEFT JOIN "Ticket" AS t ON t."createdAt" >= start_ts AND t."createdAt" < start_ts + ${interval}::INTERVAL
    GROUP by start_ts
    ORDER BY start_ts
  `;

This should work, but fails. It only works if the interval is first casted to TEXT and then to INTERVAL:

${interval}::TEXT::INTERVAL

Expected behavior

It should be possible to cast it directly to interval.

Prisma information

@prisma/cli          : 2.12.1
@prisma/client       : 2.12.1
Current platform     : darwin
Query Engine         : query-engine cf0680a1bfe8d5e743dc659cc7f08009f9587d58 (at node_modules/@prisma/engines/query-engine-darwin)
Migration Engine     : migration-engine-cli cf0680a1bfe8d5e743dc659cc7f08009f9587d58 (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core cf0680a1bfe8d5e743dc659cc7f08009f9587d58 (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary        : prisma-fmt cf0680a1bfe8d5e743dc659cc7f08009f9587d58 (at node_modules/@prisma/engines/prisma-fmt-darwin)
Studio               : 0.322.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
emcpow2commented, Jan 13, 2022

Also getting

Raw query failed. Code: `08P01`. Message: `db error: ERROR: insufficient data left in message

error in this case

        await this.prismaService.$queryRaw`
          SELECT "id"
          FROM "entities"
          WHERE "id" = ${entityId} FOR UPDATE
          `;

the problem might be in entityId: bigint

3reactions
elie222commented, Oct 10, 2021

@JClackett this workaround works from the original message:

where date > current_date - ${period}::TEXT::INTERVAL

Ran into the same issue. Still a problem but double casting not the end of the world. Thanks for the solution @Sytten.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to parameterize a query using INTERVAL - Stack Overflow
I prefer using make_interval(): SELECT * FROM detail t WHERE t.job_start_time < now() - make_interval(months => ?)
Read more >
Passing value of datatype interval in parametrized query
Looks like your parameter is passed as typed value, and I assume text or varchar . There is no implicit type cast for...
Read more >
9.9. Date/Time Functions and Operators - PostgreSQL
9.9. Date/Time Functions and Operators ; age (timestamp), interval, Subtract from current_date ; clock_timestamp (), timestamp with time zone, Current date and ...
Read more >
PostgreSQL - SQLAlchemy 1.4 Documentation
isolation_level parameter. For PostgreSQL dialects, this feature works either by making use of the DBAPI-specific features, such as psycopg2's isolation level ...
Read more >
[Solved]-Using parameter with interval in query-postgresql
You need a closed interval and do not cast to date . If necessary cast to timestamp or timestamptz depending on tstampz data...
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