{{{
from django.db import models
class M1(models.Model):
id = models.AutoField(primary_key=True,blank=True, null=True)
f1 = models.TextField(blank=True, null=True)
class Meta:
managed = False
db_table = 'm1'
class M2(models.Model):
id = models.AutoField(primary_key=True,blank=True, null=True)
f2 = models.TextField(blank=True, null=True)
class Meta:
managed = False
db_table = 'm2'
class M3(models.Model):
id = models.AutoField(primary_key=True,blank=True, null=True)
f3 = models.TextField(blank=True, null=True)
class Meta:
managed = False
db_table = 'm3'
}}}
Based on these models, I perform the following query.
{{{
o1 = M2.objects.using('default')
o2 = M1.objects.using('default')
u1 = o1.union(o2)
q = u1.order_by('-f2')
o3 = Whelped.objects.using('default')
res = q.union(o3)
print(res.count())
}}}
Unfortunately, this query crashes with a TypeError exception. The track
trace is
{{{
Traceback (most recent call last):
File "example.py", line 19, in <module>
print(res.count())
File "/home/.env/lib/python3.6/site-
packages/Django-3.2-py3.6.egg/django/db/models/query.py", line 411, in
count
return self.query.get_count(using=self.db)
File "/home/.env/lib/python3.6/site-
packages/Django-3.2-py3.6.egg/django/db/models/sql/query.py", line 517, in
get_count
number = obj.get_aggregation(using, ['__count'])['__count']
File "/home/.env/lib/python3.6/site-
packages/Django-3.2-py3.6.egg/django/db/models/sql/query.py", line 485, in
get_aggregation
outer_query.add_subquery(inner_query, using)
File "/home/.env/lib/python3.6/site-
packages/Django-3.2-py3.6.egg/django/db/models/sql/subqueries.py", line
162, in add_subquery
self.subquery, self.sub_params =
query.get_compiler(using).as_sql(with_col_aliases=True)
File "/home/.env/lib/python3.6/site-
packages/Django-3.2-py3.6.egg/django/db/models/sql/compiler.py", line 507,
in as_sql
result, params = self.get_combinator_sql(combinator,
self.query.combinator_all)
File "/home/.env/lib/python3.6/site-
packages/Django-3.2-py3.6.egg/django/db/models/sql/compiler.py", line 443,
in get_combinator_sql
if compiler.get_order_by():
File "/home/.env/lib/python3.6/site-
packages/Django-3.2-py3.6.egg/django/db/models/sql/compiler.py", line 368,
in get_order_by
for idx, (sel_expr, _, col_alias) in enumerate(self.select):
TypeError: 'NoneType' object is not iterable
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/31916>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* type: Uncategorized => Bug
* component: Uncategorized => Database layer (models, ORM)
* stage: Unreviewed => Accepted
Comment:
Thanks for this report. `ORDER BY` is not allowed in subqueries, however
it should raise a descriptive errror: `"ORDER BY not allowed in subqueries
of compound statements"`, even if a subquery is also a combined queryset.
--
Ticket URL: <https://code.djangoproject.com/ticket/31916#comment:1>
* owner: nobody => Hasan Ramezani
* status: new => assigned
* has_patch: 0 => 1
Comment:
[https://github.com/django/django/pull/13426 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/31916#comment:2>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"a046bcadbee2dc0e6c889f82c08b5a21a32359ad" a046bca]:
{{{
#!CommitTicketReference repository=""
revision="a046bcadbee2dc0e6c889f82c08b5a21a32359ad"
Fixed #31916 -- Fixed combined queryset crash when combining with ordered
combined querysets.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/31916#comment:3>