Opened 4 years ago

Closed 4 years ago

#31916 closed Bug (fixed)

Combined queryset crash on combined querysets with ordering.

Reported by: StefanosChaliasos Owned by: Hasan Ramezani
Component: Database layer (models, ORM) Version:
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have the following models.

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

Change History (3)

comment:1 by Mariusz Felisiak, 4 years ago

Component: UncategorizedDatabase layer (models, ORM)
Summary: Unexpected crash: TypeError: 'NoneType' object is not iterableCombined queryset crash on combined querysets with ordering.
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

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.

comment:2 by Hasan Ramezani, 4 years ago

Has patch: set
Owner: changed from nobody to Hasan Ramezani
Status: newassigned

comment:3 by GitHub <noreply@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In a046bca:

Fixed #31916 -- Fixed combined queryset crash when combining with ordered combined querysets.

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