[Django] #33975: Using .filter with lookup field__in=queryset where queryset contains .annotate and .alias fails

20 views
Skip to first unread message

Django

unread,
Sep 2, 2022, 4:07:41 AM9/2/22
to django-...@googlegroups.com
#33975: Using .filter with lookup field__in=queryset where queryset contains
.annotate and .alias fails
-----------------------------------------+------------------------
Reporter: gmuj | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 4.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
Here is a test case to reproduce the bug, you can add this in
tests/annotations/tests.py

{{{#!python
def test_annotation_and_alias_filter_in_subquery(self):
long_books_qs = (
Book.objects.filter(
pages__gt=400,
)
.annotate(book_annotate=Value(1))
.alias(book_alias=Value(1))
)
publisher_books_qs = (
Publisher.objects.filter(
book__in=long_books_qs
)
.values("name")
)
self.assertCountEqual(
publisher_books_qs,
[
{'name': 'Apress'},
{'name': 'Sams'},
{'name': 'Prentice Hall'},
{'name': 'Morgan Kaufmann'}
]
)
}}}

You should get this error:

{{{django.db.utils.OperationalError: sub-select returns 10 columns -
expected 1}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33975>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 2, 2022, 4:17:31 AM9/2/22
to django-...@googlegroups.com
#33975: Using .filter with lookup field__in=queryset where queryset contains
.annotate and .alias fails
-------------------------------------+-------------------------------------
Reporter: Gabriel Muj | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Gabriel Muj):

* component: Uncategorized => Database layer (models, ORM)


--
Ticket URL: <https://code.djangoproject.com/ticket/33975#comment:1>

Django

unread,
Sep 2, 2022, 4:29:46 AM9/2/22
to django-...@googlegroups.com
#33975: Using .filter with lookup field__in=queryset where queryset contains
.annotate and .alias fails
-------------------------------------+-------------------------------------
Reporter: Gabriel Muj | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => invalid


Comment:

This is a
[https://docs.djangoproject.com/en/stable/ref/models/expressions/#limiting-a
-subquery-to-a-single-column documented] and expected behavior.

--
Ticket URL: <https://code.djangoproject.com/ticket/33975#comment:2>

Django

unread,
Sep 2, 2022, 4:55:31 AM9/2/22
to django-...@googlegroups.com
#33975: Using .filter with lookup field__in=queryset where queryset contains
.annotate and .alias fails
-------------------------------------+-------------------------------------
Reporter: Gabriel Muj | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Gabriel Muj):

Using either {{{.alias}}} or {{{.annotate}}} works as expected without
using {{{.values}}} to limit to 1 column. Why is that? but using both of
them doesn't seem work.

--
Ticket URL: <https://code.djangoproject.com/ticket/33975#comment:3>

Reply all
Reply to author
Forward
0 new messages