I have a query where I prepare parameters to be passed via additionalScope for another query. These parameters are essentially a list of IDs that I want to use as a filter in the WHERE clause of the second query using IN.
The problem arises because the additionalScope is always passed as a string, which causes issues when trying to use it in the SQL query. Specifically, the string format causes syntax errors when the query is executed.
For example, I pass a list of IDs like "(1,2,3,4)" as a string, but when used in the IN clause, it leads to errors since the values are not correctly formatted for SQL.
Error: invalid input syntax for type integer: "(1,2,3,4,5,6,7,8)"
Goal: I need to pass these parameters in a way that they can be correctly interpreted by SQL as a list of integers, allowing the query to function without syntax errors.
Query sample:
select * from custos where id_centro_de_custo in {{custos}}
So, you need to pass the additional scope as an arrays of integers, something like [1,2,3,4,5] (this should be quite easy to achieve with JS, but let me know if you need guidance on this), and then in your second query change your syntax to WHERE id_centro_de_custo = any ({{custos}})
With regards to this syntax, there is something related to prepared statements that makes IN not work as usual, but to be honest I've never quite grasped why
I'm referring to a multiselect component which has [7,8] as value. However, the query, for some reason unknown to me, runs as any (7,8), removing the curly brackets and returning my result.
However if I hard code it to = any ([7,8]) I get the same error you mention.
If you use SELECT * FROM custos WHERE centro_de_custo_id = ANY ( ARRAY[1,2,3] ) it will work. The syntax is checking for the existence of the value in an array, and in Postgres, you can't just pass a string, you need to wrap it in ARRAY for it to be treated as such.
Thanks a lot for the tips. I ended up moving jobs so I could not finish testing the application although I'm starting a new one now and I intend to use Retool here, I'll come back to it and let you guys now how (hopefully) I sorted this out