$ the-wire · showcase
Django fixes two query regressions in ORM
By RepoJournal · Filed · About Django
Two regressions in the Django ORM, one in distinct() on PostgreSQL and one in in_bulk(), are fixed in pull requests submitted this week.
Django's ORM has two targeted fixes in flight [1] [2]. QuerySet.distinct() crashed on PostgreSQL with "SELECT DISTINCT ON expressions must match initial" when two lookup paths resolved to the same column and values() selected it at multiple positions. The crash was a regression introduced in 65ad4ade; the fix aligns the DISTINCT ON binding with the first selected position. Separately, QuerySet.in_bulk() dropped selected annotations and extra-select aliases when the mapping key field wasn't selected, because it rebuilt values() and values_list() projections from query.values_select instead of the complete selected projection. The patch rebuilds from query.selected and adds regression tests.
A third ORM fix prevents iterator right-hand sides from being consumed in annotation lookups [3]. When the left-hand side of an in or range lookup is an annotation, the right-hand side iterator is now materialized once before checking its values for expressions, so it can't be exhausted prematurely. The fix covers both lookup types and updates the Django 6.1.1 release notes.
Documentation gets two additions: the core developers page on djangoproject.com now links to the historical Django Core Developers page, addressing issue #2747 [4] [5]. Django's docs also gain the remaining community package mentions, covering everything except the environment configuration section [6]. One more doc change clarifies object-level permission checks in the admin [7], aiming to reduce misplaced security reports. Note that the community packages PR has an AI assistance disclosure, though the assistant checkbox state is not confirmed.
On the infrastructure side, django-asv benchmark results for ubuntu-latest were added to the repository [8].
Action items
- → Test querysets using distinct() with duplicated values() selections on PostgreSQL when the fix lands django/django [plan]
- → Review in_bulk() calls that rely on annotations or extra selects after the fix django/django [plan]
References
- [1] Fixed #37222 -- Fixed QuerySet.distinct() crash on duplicated selections. ↗ django/django
- [2] Fixed #37312, Refs #36605 -- Fixed annotation preservation and key selection in in_bulk(). ↗ django/django
- [3] Fixed #37311 -- Prevented consumption of rhs iterators in annotation lookups. ↗ django/django
- [4] Added core developers link to teams page (#2808) ↗ django/djangoproject.com
- [5] Updates team page to link django core developers ↗ django/djangoproject.com
- [6] Added remaining community package mentions to the documentation. ↗ django/django
- [7] Clarified object-level permission checks in the admin. ↗ django/django
- [8] Results for ubuntu-latest added [skip ci] ↗ django/django-asv