diff options
| author | can <cansarigol@derinbilgi.com.tr> | 2019-05-02 20:11:26 +0300 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-05-03 07:37:37 +0200 |
| commit | 567b9928a3ad37e95b9ae17ec41342daa6968739 (patch) | |
| tree | 675964a28df126c9de1f77b0df74700929a4f9f1 | |
| parent | ef082ebb84f00e38af4e8880d04e8365c2766d34 (diff) | |
Fixed #29692 -- Fixed removing ordering parts for multiline RawSQL expressions.
| -rw-r--r-- | django/db/models/sql/compiler.py | 3 | ||||
| -rw-r--r-- | tests/expressions/tests.py | 23 |
2 files changed, 25 insertions, 1 deletions
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index 7649c39262..23bc339e78 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -32,7 +32,8 @@ class SQLCompiler: self.select = None self.annotation_col_map = None self.klass_info = None - self.ordering_parts = re.compile(r'(.*)\s(ASC|DESC)(.*)') + # Multiline ordering SQL clause may appear from RawSQL. + self.ordering_parts = re.compile(r'^(.*)\s(ASC|DESC)(.*)', re.MULTILINE | re.DOTALL) self._meta_ordering = None def setup_query(self): diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py index c22ff34ee8..a8897fa069 100644 --- a/tests/expressions/tests.py +++ b/tests/expressions/tests.py @@ -384,6 +384,29 @@ class BasicExpressionsTests(TestCase): ) self.assertSequenceEqual(mustermanns_by_seniority, [self.max, mary]) + def test_order_by_multiline_sql(self): + raw_order_by = ( + RawSQL(''' + CASE WHEN num_employees > 1000 + THEN num_chairs + ELSE 0 END + ''', []).desc(), + RawSQL(''' + CASE WHEN num_chairs > 1 + THEN 1 + ELSE 0 END + ''', []).asc() + ) + for qs in ( + Company.objects.all(), + Company.objects.distinct(), + ): + with self.subTest(qs=qs): + self.assertSequenceEqual( + qs.order_by(*raw_order_by), + [self.example_inc, self.gmbh, self.foobar_ltd], + ) + def test_outerref(self): inner = Company.objects.filter(point_of_contact=OuterRef('pk')) msg = ( |
