Opened 5 years ago
Closed 5 years ago
#32693 closed Cleanup/optimization (fixed)
case sensitive issue on subquery aliases generated
| Reported by: | ecogels | Owned by: | Hasan Ramezani |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 3.2 |
| Severity: | Normal | Keywords: | subquery |
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | yes | UI/UX: | no |
Description
When generating aliases for subqueries, django uses 2 different cases:
'Col%d' https://github.com/django/django/blob/187118203197801c6cb72dc8b06b714b23b6dd3d/django/db/models/sql/compiler.py#L557
Here is a concrete example, using --v 2 --debug-sql queries.tests.Queries6Tests.test_distinct_ordered_sliced_subquery_aggregation.
SELECT COUNT(*) FROM (SELECT "subquery"."col1", "subquery"."col2", "subquery"."col3", "subquery"."col4" FROM (SELECT DISTINCT "queries_tag"."id" AS Col1, "queries_tag"."name" AS Col2, "queries_tag"."parent_id" AS Col3, "queries_tag"."category_id" AS Col4, "queries_namedcategory"."name" AS Col5 FROM "queries_tag" LEFT OUTER JOIN "queries_namedcategory" ON ("queries_tag"."category_id" = "queries_namedcategory"."dumbcategory_ptr_id") ORDER BY "queries_namedcategory"."name" ASC LIMIT 3) subquery) subquery;
This would fail on a database with case sensitive column names.
It will be trivial to fix if deemed necessary.
Change History (7)
comment:1 by , 5 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|---|
| Type: | Bug → Cleanup/optimization |
comment:2 by , 5 years ago
| Has patch: | set |
|---|---|
| Owner: | changed from to |
| Status: | new → assigned |
comment:3 by , 5 years ago
| Needs tests: | set |
|---|
comment:5 by , 5 years ago
| Needs tests: | unset |
|---|
comment:6 by , 5 years ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
It's not an issue in practice because SQLite, MySQL, and PostgreSQL all fold ambiguous aliases to lower case, try the following in their respective shell
SQlite and Postgres
MySQL
Oracle folds uppercase (as per the SQL standard) though but the quoted usage happens to uppercase the identifier which transforms the query to
Oracle
Which also works.
I think we should still perform a cleanup here for the sake of consistency with two adjustments:
colalias in lowercaseconnection.ops.quote_nameon the generated aliasBoth of these changes should be made to the first instance you pointed at. Do you feel comfortable submitting a Github PR that does that?