Comparing django:8b020f2e64...dvarrazzo:psycopg3-4.1 · django/django
Commits on Nov 22, 2021
-
Add supports_order_column_alias database feature.
This features is necessary for database drivers implementing server-side binding (at least it is for PostgreSQL). The problem is around sorting on expressions. For instance, with the query: SELECT sum(value) FROM testdata GROUP BY left(name, %s) ORDER BY left(name, %s) If the query is composed client-side, the server is able to infer that the group by and the order by are the same expression. If the argument is passed to the server as a single copy and several placeholders refer to it, it works too, for instance the following works well: cursor.execute( """ SELECT sum(value) FROM testdata GROUP BY left(name, %(num)s) ORDER BY left(name, %(num)s) """, {"num": 3}) But if only positional arguments are passed, cursor.execute( """ SELECT sum(value) FROM testdata GROUP BY left(name, %s) ORDER BY left(name, %s) """, [3, 3]) then PostgreSQL will not be able to infer that the group by and the order by are the same expression and will fail with column "testdata.name" must appear in the GROUP BY clause or be used in an aggregate function If a backend is declared 'supports_order_column_alias', then the query is rewritten as: cursor.execute( """ SELECT sum(value) FROM testdata GROUP BY left(name, %s) ORDER BY 1 """, [3]) avoiding the problem.