Opened 3 weeks ago

Closed 13 days ago

Last modified 5 days ago

#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 Dmitri, 3 weeks ago

Has patch: set

comment:3 by Jacob Walls, 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 Ulrich Görtz, 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 Dmitri, 3 weeks ago

Owner: set to Dmitri
Status: new → assigned

comment:6 by Sarah Boyce, 2 weeks ago

Triage Stage: Accepted → Ready for checkin

comment:7 by Sarah Boyce <42296566+sarahboyce@…>, 13 days ago

Resolution: → fixed
Status: assigned → closed

In 10b25774:

Fixed #37505 -- Fixed crash when combining querysets with Meta.ordering referencing related fields.

Thank you Ulrich Görtz for the report.

Regression in 087bb9e8f3478d53f12b1737af865992af17c5f2.

comment:8 by Sarah Boyce <42296566+sarahboyce@…>, 13 days ago

In 75f35ee:

[6.1.x] Fixed #37505 -- Fixed crash when combining querysets with Meta.ordering referencing related fields.

Thank you Ulrich Görtz for the report.

Regression in 087bb9e8f3478d53f12b1737af865992af17c5f2.

Backport of 10b25774fabaed05fd400968ba6a01b15318bdea from main.

comment:9 by Jacob Walls <jacobtylerwalls@…>, 5 days ago

In fe7b7dc:

Refs #37505 -- Skipped test_combined_default_ordering_by_related_fields on Oracle.

This test raises ORA-00907: missing right parenthesis. As Simon proposed
when reviewing fbe902d4a4b8b9dcb371509b25ba8feff3852e64, we could elide
ordering in combined queries when it is not meaningful:

"Then at compilation time, if the ordering was meaningful at combination
time and thus was preserved, and the target backend doesn't support it
we error out loudly based on the feature flags."

However, this behavior is the same on stable/6.0.x, so we defer it for now.

Omission in 10b25774fabaed05fd400968ba6a01b15318bdea.

comment:10 by Jacob Walls <jacobtylerwalls@…>, 5 days ago

In 49e366a:

[6.1.x] Refs #37505 -- Skipped test_combined_default_ordering_by_related_fields on Oracle.

This test raises ORA-00907: missing right parenthesis. As Simon proposed
when reviewing fbe902d4a4b8b9dcb371509b25ba8feff3852e64, we could elide
ordering in combined queries when it is not meaningful:

"Then at compilation time, if the ordering was meaningful at combination
time and thus was preserved, and the target backend doesn't support it
we error out loudly based on the feature flags."

However, this behavior is the same on stable/6.0.x, so we defer it for now.

Omission in 10b25774fabaed05fd400968ba6a01b15318bdea.

Backport of fe7b7dc8b47937e6e4e7979a8b2ec1ba0fa122b2 from main.

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