Postgres parameterized interval is passed incorrectly in raw query
See original GitHub issueBug 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:
- Created 3 years ago
- Reactions:3
- Comments:11 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Also getting
error in this case
the problem might be in
entityId: bigint
@JClackett this workaround works from the original message:
Ran into the same issue. Still a problem but double casting not the end of the world. Thanks for the solution @Sytten.