#37350 assigned Bug

distinct(*fields) does not fathom aliases containing the lookup separator: "__"

Reported by: Jacob Walls Owned by: Django Sprints
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

We are gradually improving the situation with respect to the treatment of annotation aliases containing the lookup separator, see PR discussion. This ticket is for .distinct(*fields).

When an alias is provided to .distinct(*fields), is it split on LOOKUP_SEP before checking against the alias map, meaning it will miss an alias that contains it.

Reproducer:

qs = Person.objects.annotate(my__val=models.Value(1)).distinct("my__val")
print(qs)
  File "/django-ref/django/db/models/sql/query.py", line 1854, in names_to_path
    raise FieldError(
django.core.exceptions.FieldError: Cannot resolve keyword 'my' into field. Choices are: id, my__val, name

The helper from 73cc09f14f13fedddc14d6ba5b287cb33c24e4a4 could be used as part of fixing this, so that we don't omit any nuance with FilteredRelation.

Using the lookup separator in an annotation alias sounds a little unorthodox, but it's present in the default alias generated by .aggregate(), and we've taken reports on it before. Also, if you let users choose aliases in configurable reports, it's bound to come up, and we've taken issues on that.

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top