question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

postgres `IN` query value substitution for lists is incorrect

See original GitHub issue
In [2]: frappe.db.sql("select name from `tabItem` where name in %s", (["abc", "xyz"],), debug=1)
select name from "tabItem" where name in ARRAY['abc','xyz']
Syntax error in query:
select name from "tabItem" where name in %s
---------------------------------------------------------------------------
SyntaxError                               Traceback (most recent call last)
File <ipython-input-2-6d9b73d925f2>:1, in <cell line: 1>()
----> 1 frappe.db.sql("select name from `tabItem` where name in %s", (["abc", "xyz"],), debug=1)

File ~/benches/pg/apps/frappe/frappe/database/postgres/database.py:121, in PostgresDatabase.sql(self, query, values, *args, **kwargs)
    120 def sql(self, query, values=(), *args, **kwargs):
--> 121         return super(PostgresDatabase, self).sql(
    122                 modify_query(query), modify_values(values), *args, **kwargs
    123         )

File ~/benches/pg/apps/frappe/frappe/database/database.py:185, in Database.sql(self, query, values, as_dict, as_list, formatted, debug, ignore_ddl, as_utf8, auto_commit, update, explain, run, pluck)
    182 if not isinstance(values, (dict, tuple, list)):
    183         values = (values,)
--> 185 self._cursor.execute(query, values)
    187 if frappe.flags.in_migrate:
    188         self.log_touched_tables(query, values)

SyntaxError: syntax error at or near "ARRAY"
LINE 1: select name from "tabItem" where name in ARRAY['abc','xyz']
                                                 ^

Hint: List values don’t work but tuple values do.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
phot0ncommented, Jun 17, 2022

(hoping we don’t use it in future as well 📿 🙏 )

2reactions
phot0ncommented, Jun 17, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

7.8. WITH Queries (Common Table Expressions) - PostgreSQL
Recursive Query Evaluation​​ So long as the working table is not empty, repeat these steps: Evaluate the recursive term, substituting the current contents...
Read more >
Documentation: 15: SELECT - PostgreSQL
When GROUP BY is present, or any aggregate functions are present, it is not valid for the SELECT list expressions to refer to...
Read more >
Documentation: 15: 4.2. Value Expressions - PostgreSQL
Value expressions are used in a variety of contexts, such as in the target list of the SELECT command, as new column values...
Read more >
Documentation: 15: 43.5. Basic Statements - PostgreSQL
PL/pgSQL variables will be substituted into the query just as described above, and the plan is cached in the same way. Also, the...
Read more >
Documentation: 15: 9.7. Pattern Matching - PostgreSQL
operators, functions are available to extract or replace matching substrings ... Table 9.16 lists the available operators for pattern matching using POSIX ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found