Skip to content

Postgresql Raw Query Tuple Parameter 500 error on explain statement #1482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tom-price opened this issue Jul 10, 2021 · 2 comments · Fixed by #1722
Closed

Postgresql Raw Query Tuple Parameter 500 error on explain statement #1482

tom-price opened this issue Jul 10, 2021 · 2 comments · Fixed by #1722

Comments

@tom-price
Copy link

tom-price commented Jul 10, 2021

I've got a .raw query with the following filter on a JSONB column named data:
WHERE data ->> 'key' IN %s

The %s is populated by taking the list and passing it in as follows:
ModelName.objects.raw(query_with_params, [tuple(list_of_values)])

When viewing the debug pane the SQL generated for the relevant section looks like:
WHERE data ->> 'key' IN ('value_1', 'value_2', 'value_3')

When trying to view the explain of the query a 500: Internal Server Error results due to a pscyopg2.errors.syntax error on the following SQL:
data ->> 'key' IN ARRAY['value_1','value_2','value_3',...

It looks like there's some issue in what populates the params for this line where the passed in tuple is being turned back into a list as lists are turned into ARRAYS not collections.

I was able to get it to work by with the following changes:
WHERE data ->> 'key' = ANY(%s)
and
ModelName.objects.raw(query_with_params, [list_of_values]) where type(list_of_values) is <class 'list'>

@auvipy
Copy link
Contributor

auvipy commented Oct 25, 2021

are you able to provide a fix?

@tim-schilling
Copy link
Member

This can be addressed by modifying _quote_params to return a tuple when params is a tuple.

The following code in our test app will trigger a SyntaxError:

list(PostgresJSON.objects.raw("SELECT * FROM tests_postgresjson WHERE field ->> 'key' IN %s", [("a", "b'")]))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants