1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Fixed #28848 -- Fixed SQLite/MySQL crash when ordering by a filtered subquery that uses nulls_first/nulls_last.

This commit is contained in:
Raphael Michel 2017-11-26 17:32:17 +01:00 committed by Tim Graham
parent ad5f33ee03
commit 616f468760
3 changed files with 26 additions and 1 deletions

View File

@ -1106,6 +1106,7 @@ class OrderBy(BaseExpression):
} }
placeholders.update(extra_context) placeholders.update(extra_context)
template = template or self.template template = template or self.template
params *= template.count('%(expression)s')
return (template % placeholders).rstrip(), params return (template % placeholders).rstrip(), params
def as_sqlite(self, compiler, connection): def as_sqlite(self, compiler, connection):

View File

@ -21,3 +21,6 @@ Bugfixes
* Made ``QuerySet.iterator()`` use server-side cursors on PostgreSQL after * Made ``QuerySet.iterator()`` use server-side cursors on PostgreSQL after
``values()`` and ``values_list()`` (:ticket:`28817`). ``values()`` and ``values_list()`` (:ticket:`28817`).
* Fixed crash on SQLite and MySQL when ordering by a filtered subquery that
uses ``nulls_first`` or ``nulls_last`` (:ticket:`28848`).

View File

@ -1,7 +1,7 @@
from datetime import datetime from datetime import datetime
from operator import attrgetter from operator import attrgetter
from django.db.models import F from django.db.models import DateTimeField, F, Max, OuterRef, Subquery
from django.db.models.functions import Upper from django.db.models.functions import Upper
from django.test import TestCase from django.test import TestCase
@ -138,6 +138,27 @@ class OrderingTests(TestCase):
[self.a1, self.a2, self.a4, self.a3], [self.a1, self.a2, self.a4, self.a3],
) )
def test_orders_nulls_first_on_filtered_subquery(self):
Article.objects.filter(headline='Article 1').update(author=self.author_1)
Article.objects.filter(headline='Article 2').update(author=self.author_1)
Article.objects.filter(headline='Article 4').update(author=self.author_2)
Author.objects.filter(name__isnull=True).delete()
author_3 = Author.objects.create(name='Name 3')
article_subquery = Article.objects.filter(
author=OuterRef('pk'),
headline__icontains='Article',
).order_by().values('author').annotate(
last_date=Max('pub_date'),
).values('last_date')
self.assertQuerysetEqualReversible(
Author.objects.annotate(
last_date=Subquery(article_subquery, output_field=DateTimeField())
).order_by(
F('last_date').asc(nulls_first=True)
).distinct(),
[author_3, self.author_1, self.author_2],
)
def test_stop_slicing(self): def test_stop_slicing(self):
""" """
Use the 'stop' part of slicing notation to limit the results. Use the 'stop' part of slicing notation to limit the results.