#28497 closed Bug (fixed)
QuerySet.filter() with a sliced QuerySet crashes
Reported by: | Sergey Fedoseev | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Release blocker | Keywords: | |
Cc: | Simon Charette | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
PersonSkill.objects.filter(skill=Skill.objects.all()[:1]) ProgrammingError: ОШИБКА: подзапрос должен вернуть только один столбец (subquery should return single column) LINE 1: ...nskill" WHERE "test_app_personskill"."skill_id" = (SELECT "t...
Introduced in ec50937bcbe160e658ef881021402e156beb0eaf.
Change History (10)
comment:1 by , 7 years ago
Severity: | Normal → Release blocker |
---|---|
Summary: | filtering with non-values QuerySet crashes → QuerySet.filter() with a sliced QuerySet crashes |
Triage Stage: | Unreviewed → Accepted |
Version: | 1.11 → master |
comment:2 by , 7 years ago
Cc: | added |
---|
follow-up: 7 comment:5 by , 7 years ago
Here's a regression test. It's not clear to me why this shouldn't be allowed. What should the error message say?
-
tests/lookup/tests.py
diff --git a/tests/lookup/tests.py b/tests/lookup/tests.py index 25098af..7eeaa26 100644
a b class LookupTests(TestCase): 138 138 with self.assertNumQueries(expected_num_queries): 139 139 self.assertEqual(Author.objects.in_bulk(authors.keys()), authors) 140 140 141 def test_filter_with_sliced(self): 142 self.assertSequenceEqual( 143 Article.objects.filter(author=Author.objects.all()[:1]), 144 [self.a4, self.a2, self.a3, self.a1], 145 ) 146 141 147 def test_values(self): 142 148 # values() returns a list of dictionaries instead of object instances -- 143 149 # and you can specify which fields you want to retrieve.
comment:6 by , 7 years ago
What should the error message say?
That __in
should be used with multiple elements or a single model instance should be passed? Maybe we could fix the issue and officialize and support as well.
Any thoughts Sergey?
comment:7 by , 7 years ago
Note:
See TracTickets
for help on using tickets.
We should try to error out with a more appropriate message but I don't think we ever officially supported passing a queryset to an
__exact
lookup. I vaguely remember a ticket about it but I can't find it anymore.I think you should be using an
__in
lookup instead as your query will fail withmore than one row returned by a subquery used as an expression
as soon as the table backing up yourSkill
model has more than two rows.