Closed philipm-domuso closed 8 months ago
You can't add postgres protocol level parameters inside the crosstab string, since it's just a string.
What you can do is either use sql.unsafe
inside the parameter, or maybe you can use format like this:
const ids = [1,2]
await sql`
select
*
from public.crosstab(
format(
$$
select
day_of_month,
months_ago,
sum(amount) as day_amount
from payments
where
pay_id = any(%L::text[])
group by date_day, day_of_month
order by 1,2
$$,
${ '{' + ids.join(',') + '}' }
),
$$ values('months_ago_0'), ('months_ago_1'), ('months_ago_2') $$
) as ct (
date_of_month int,
months_ago_0 numeric,
months_ago_1 numeric,
months_ago_2 numeric
)
`
I think this may be a more advanced case, or a PG parametrized query issue, but I wanted to see if you have any advice on how to approach this.
Code:
The error is
could not determine data type of parameter $1
. Logging with{debug: console.log}
produces the following PG query:Is
sql.unsafe
my only option for this case?