﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
37308	Lookups on annotation aliases in .values() and .order_by() resolve unexpectedly	Annabelle Wiegart		"Doing a lookup on an annotation alias results in an unexpected name resolution. When calling e.g. `.values(alias + ""__pk"")`, Query.names_to_path() checks if `f""{alias}__pk""` is the name of an annotation. Otherwise it tries to resolve the name into field plus lookup. This might be unexpected. While allowing lookups on aliases may add too much complexity and therefore not be desirable, it would probably be helpful to at least show a warning when `.values()` or `.order_by()` is called on an expression containing an alias name and `__`.

The issue has been discussed in [https://github.com/django/django/pull/21803#discussion_r3816836443 PR21803] for #36945.

[https://dryorm.xterm.info/lookup-on-alias ORM fiddle]

Reproducer:

{{{#!div style=""font-size: 80%""
  {{{#!python
from django.db import models
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType


class Person(models.Model):
    name = models.CharField(max_length=100)
    creator = models.ForeignKey(User, models.CASCADE, null=True)
    ct = models.ForeignKey(ContentType, models.CASCADE)


def run():
    admin = User.objects.create(username='admin')
    person_ct = ContentType.objects.get_for_model(Person)
    Person.objects.create(creator=admin, name=""Claude"", ct=person_ct)
    # resolves correctly
    qs1 = Person.objects.all().values(""creator__pk"")
    print(qs1)
    
    alias = ""myalias""
    # raises FieldError
    qs2 = Person.objects.annotate(**{alias: models.F(""creator"")}).values(alias + ""__pk"")
    print(qs2)
    # raises FieldError
    qs3 = Person.objects.annotate(
        **{alias: models.FilteredRelation(""creator"", condition=models.Q(creator__isnull=False))}
    ).values(alias + ""__pk"")
    print(qs3)
  }}}
}}}

Output:

{{{
django.core.exceptions.FieldError: Cannot resolve keyword 'myalias' into field. Choices are: creator, creator_id, ct, ct_id, id, name
}}}
"	Cleanup/optimization	new	Database layer (models, ORM)	6.1	Normal		alias, values, order_by	Annabelle Wiegart	Unreviewed	0	0	0	0	0	0
