#34957 closed Bug (needsinfo)

Cannot filter over annotated "FilteredRelation" since Django 5

Reported by: younes-chaoui Owned by: nobody
Component: Uncategorized Version: 5.0
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 (last modified by younes-chaoui)

After updating to django 5.0b1 I get RecursionError when filtering over annotated "FilteredRelation".

This is an example to reproduce it :

from django.db.models import F, FilteredRelation, Q
visites = (
    Visite.objects.all()
    .annotate(
        etat_at_date_de_visite=FilteredRelation(
            "site__siteusage__etats",
            condition=Q(site__siteusage__etats__date_valeur=F("date_de_visite")),
        )
    )
    .filter(etat_at_date_de_visite__isnull=False)
)
print(visites)

Tracelog

 File "/home/younes/.local/share/virtualenvs/new-p1-back-ouzi8-CM/lib/python3.11/site-packages/django/db/models/sql/query.py", line 1913, in setup_joins
    alias = self.join(connection, reuse=reuse)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/younes/.local/share/virtualenvs/new-p1-back-ouzi8-CM/lib/python3.11/site-packages/django/db/models/sql/query.py", line 1133, in join
    join.filtered_relation = filtered_relation.resolve_expression(
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/younes/.local/share/virtualenvs/new-p1-back-ouzi8-CM/lib/python3.11/site-packages/django/db/models/query_utils.py", line 463, in resolve_expression
    clone.resolved_condition = query.build_filter(
                               ^^^^^^^^^^^^^^^^^^^
  File "/home/younes/.local/share/virtualenvs/new-p1-back-ouzi8-CM/lib/python3.11/site-packages/django/db/models/sql/query.py", line 1458, in build_filter
    return self._add_q(
           ^^^^^^^^^^^^
  File "/home/younes/.local/share/virtualenvs/new-p1-back-ouzi8-CM/lib/python3.11/site-packages/django/db/models/sql/query.py", line 1631, in _add_q
    child_clause, needed_inner = self.build_filter(
                                 ^^^^^^^^^^^^^^^^^^
  File "/home/younes/.local/share/virtualenvs/new-p1-back-ouzi8-CM/lib/python3.11/site-packages/django/db/models/sql/query.py", line 1481, in build_filter
    lookups, parts, reffed_expression = self.solve_lookup_type(arg, summarize)
                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/younes/.local/share/virtualenvs/new-p1-back-ouzi8-CM/lib/python3.11/site-packages/django/db/models/sql/query.py", line 1293, in solve_lookup_type
    _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/younes/.local/share/virtualenvs/new-p1-back-ouzi8-CM/lib/python3.11/site-packages/django/db/models/sql/query.py", line 1774, in names_to_path
    pathinfos = field.get_path_info(filtered_relation)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/younes/.local/share/virtualenvs/new-p1-back-ouzi8-CM/lib/python3.11/site-packages/django/db/models/fields/reverse_related.py", line 241, in get_path_info
    return self.field.get_reverse_path_info(filtered_relation)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/younes/.local/share/virtualenvs/new-p1-back-ouzi8-CM/lib/python3.11/site-packages/django/db/models/fields/related.py", line 862, in get_reverse_path_info
    PathInfo(
  File "<string>", line 1, in <lambda>
RecursionError: maximum recursion depth exceeded while calling a Python object

Change History (6)

comment:1 by younes-chaoui, 13 months ago

Description: modified (diff)

comment:2 by younes-chaoui, 13 months ago

Description: modified (diff)

comment:3 by Mariusz Felisiak, 13 months ago

Please attach your models definition.

comment:4 by younes-chaoui, 13 months ago

Description: modified (diff)

comment:5 by younes-chaoui, 13 months ago

Description: modified (diff)

comment:6 by Mariusz Felisiak, 13 months ago

Resolution: needsinfo
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top