TypedSQL Cannot Determine Data Parameter for Array

I'm attempting to create a filter system that uses $queryRawTyped() . My frontend will have 5+ multi-select string filters. Each of these filters is to check if an element has any of the selected values. If the array is empty, it should not filter by that item.
SELECT *
FROM "Student" stu
WHERE
($1 IS NULL OR ARRAY_LENGTH($1, 1) = 0) OR stu."name" = ANY($1);
SELECT *
FROM "Student" stu
WHERE
($1 IS NULL OR ARRAY_LENGTH($1, 1) = 0) OR stu."name" = ANY($1);
This causes the following error when generating the TypedSQL types ERROR: could not determine data type of parameter $1 Any suggestions on how I can use TypedSQL to achieve this functionality?
2 Replies
Prisma AI Help
Skipping coffee for instant knowledge! Let's brew that AI answer now. The error ERROR: could not determine data type of parameter $1 occurs because TypedSQL (and PostgreSQL) cannot infer the type of an untyped parameter when used as an array. In TypedSQL, you cannot manually specify argument types for array parameters in the SQL file comments; you must rely on type inference, which only works if the context makes the type clear to PostgreSQL and Prisma's generator. Your current query does not provide enough context for type inference, leading to the error you see. How to fix: Explicitly cast the parameter to the correct array type in your SQL. For example, if you expect an array of text:
SELECT *
FROM "Student" stu
WHERE
($1::text[] IS NULL OR array_length($1::text[], 1) = 0 OR stu."name" = ANY($1::text[]));
SELECT *
FROM "Student" stu
WHERE
($1::text[] IS NULL OR array_length($1::text[], 1) = 0 OR stu."name" = ANY($1::text[]));
This explicit cast ($1::text[]) allows both PostgreSQL and TypedSQL to infer the parameter type correctly, resolving the error during client generation and at runtime. When you call the generated function, pass a string array as the argument. Example usage in TypeScript:
const names: string[] = []; // or ['Alice', 'Bob']
const students = await prisma.$queryRawTyped(getStudentsByNames(names));
const names: string[] = []; // or ['Alice', 'Bob']
const students = await prisma.$queryRawTyped(getStudentsByNames(names));
Note:
- You do not need to add a -- @param comment for array parameters; manual typing is not supported for arrays in TypedSQL, so rely on the cast for inference instead Defining argument types in your SQL files. - This approach is confirmed to work for array parameters in TypedSQL Passing Arguments to TypedSQL Queries. If you have multiple filters, repeat this pattern for each parameter, casting each to the appropriate array type. ✅ Great, thanks for letting us know that this solved your question!
Kale Leafy
Kale LeafyOP4w ago
The field I am checking is an enum. When I attempt to cast it ERROR: type "exampleEnum[]" does not exist

Did you find this page helpful?