#33309: DISTINCT ON fails with mixed-case field aliases
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
Christophe Thiery |
Type: | Status: new
Uncategorized |
Component: Database | Version: 3.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
If you pass an aliased field name to distinct(), it will fail if the alias
has some capital letters.
{{{
from django.contrib.auth.models import User
from django.db.models import F
User.objects.annotate(the_alias=F('first_name')).values('the_alias',
'id').order_by('the_alias', 'id').distinct('the_alias')
# Works
User.objects.annotate(theAlias=F('first_name')).values('theAlias',
'id').order_by('theAlias', 'id').distinct('theAlias')
# Fails with:
# ProgrammingError: column "thealias" does not exist
# LINE 1: SELECT DISTINCT ON (theAlias) "auth_user"."id",
"auth_user"."first_name" AS "theAlias"...
}}}
It looks like the DISTINCT ON clause in the generated SQL is missing
double quotes.
Tested on Django 3.2.9 and postgres 12.8.
--
Ticket URL: <https://code.djangoproject.com/ticket/33309>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/063.37eeb609ab4904678c8d4616c286ef84%40djangoproject.com.