#37505 closed Bug (fixed)
TypeError in Query.add_select_col() when combining QuerySets with Meta.ordering referencing related model fields
| Reported by: | Ulrich Görtz | Owned by: | Dmitri |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 6.1 |
| Severity: | Release blocker | Keywords: | |
| Cc: | Ulrich Görtz | Triage Stage: | Ready for checkin |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
The following problem occurs with Django 6.1, PostgreSQL, Python 3.14.
For a model (with a regular primary key) and Meta.ordering referencing fields through a foreign key (ordering = ('user__lastname', ), say), QuerySet.difference() applied to two QuerySets of that model (e.g., as a trivial example, A.objects.all().difference(A.objects.all())) throws a TypeError during SQL compilation because query.selected is None.
Adding .order_by() without arguments to the difference method resolves the problem.
The same query worked in Django 6.0. The problem might be related to #37466
Thanks for your work.
Change History (10)
comment:1 by , 3 weeks ago
| Has patch: | set |
|---|
comment:2 by , 3 weeks ago
comment:3 by , 3 weeks ago
| Severity: | Low → Release blocker |
|---|---|
| Summary: | Django 6.1: TypeError in Query.add_select_col() when combining QuerySets with Meta.ordering referencing related model fields → TypeError in Query.add_select_col() when combining QuerySets with Meta.ordering referencing related model fields |
| Triage Stage: | Unreviewed → Accepted |
Thanks, for the report, we have:
File "/usr/local/lib/python3.12/site-packages/django/db/models/sql/compiler.py", line 87, in pre_sql_setup order_by = self.get_order_by() ^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/django/db/models/sql/compiler.py", line 526, in get_order_by self.query.add_select_col(resolved, col_alias) File "/usr/local/lib/python3.12/site-packages/django/db/models/sql/query.py", line 2240, in add_select_col self.selected[name] = len(self.select) - 1 ~~~~~~~~~~~~~^^^^^^ TypeError: 'NoneType' object does not support item assignment
This feels like a judgment call about whether it's a regression, since the effect of 087bb9e8f3478d53f12b1737af865992af17c5f2 was to simply drive more traffic into a pre-existing bug, but I think it's fair to treat it as one here.
Do you want to submit a PR? (The one that was attached came in before triage, so we're happy to continue with one from you if you prefer.)
comment:4 by , 3 weeks ago
Thanks for the reply, good to see that my report was useful. I do not feel sufficiently competent to submit a PR myself, so I would be happy if you proceed with the one submitted by Dmitri.
comment:5 by , 3 weeks ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
comment:6 by , 2 weeks ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
PR: https://github.com/django/django/pull/22296