Opened 8 months ago

Closed 8 months ago

#35157 closed Bug (duplicate)

"AttributeError: 'QuerySet' object has no attribute 'copy'" combining FilteredRelation with subquery

Reported by: Javier Ayres Owned by: nobody
Component: Database layer (models, ORM) Version: 5.0
Severity: Normal Keywords:
Cc: Javier Ayres Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Starting with Django 5.0 I see the mentioned error in a query that combines a FilteredRelation with a subquery in its condition. A new project created with Django 5.0.1 and the following models will exhibit this bug.

models.py

from django.db import models


class Student(models.Model):
    insert_time = models.DateTimeField(auto_now_add=True)


class StudentList(models.Model):
    archived = models.BooleanField(default=False)

    def __str__(self):
        return self.name


class IncludedStudent(models.Model):
    student = models.ForeignKey(Student, on_delete=models.CASCADE)
    student_list = models.ForeignKey(StudentList, on_delete=models.CASCADE)
    insert_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        unique_together = ('student', 'student_list')

sample code to execute in the shell

Python 3.11.7 (main, Jan 17 2024, 06:37:55) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from app.models import *
>>> from django.db.models import FilteredRelation, Q
>>> filter = IncludedStudent.objects.filter(Q(student_list__archived=False)).values_list('id', flat=True)
>>> Student.objects.annotate(filteredincludedstudent=FilteredRelation('includedstudent', condition=Q(includedstudent__id__in=filter)))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python3.11/site-packages/django/db/models/manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/models/query.py", line 1630, in annotate
    return self._annotate(args, kwargs, select=True)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/models/query.py", line 1676, in _annotate
    clone.query.add_filtered_relation(annotation, alias)
  File "/usr/local/lib/python3.11/site-packages/django/db/models/sql/query.py", line 1682, in add_filtered_relation
    filtered_relation.condition = rename_prefix_from_q(
                                  ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/models/sql/query.py", line 120, in rename_prefix_from_q
    [get_child_with_renamed_prefix(prefix, replacement, c) for c in q.children],
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/models/sql/query.py", line 120, in <listcomp>
    [get_child_with_renamed_prefix(prefix, replacement, c) for c in q.children],
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/models/sql/query.py", line 100, in get_child_with_renamed_prefix
    rhs = get_child_with_renamed_prefix(prefix, replacement, rhs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/models/sql/query.py", line 108, in get_child_with_renamed_prefix
    child = child.copy()
            ^^^^^^^^^^
AttributeError: 'QuerySet' object has no attribute 'copy'
>>> 

Change History (1)

comment:1 by Mariusz Felisiak, 8 months ago

Resolution: duplicate
Status: newclosed

Duplicate of #35135 (will be fixed in Django 5.0.2).

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