Skip to content

Commit 587d8a0

Browse files
authored
Fix using reserved words in order_by (#1900)
1 parent 7cb58d0 commit 587d8a0

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Changelog
1515
Fixed
1616
^^^^^
1717
- Fix model with multi m2m fields generates wrong references name (#1897)
18+
- Fix using reserved words in order_by (#1900)
1819

1920
0.24.1
2021
------

tests/test_order_by.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from tortoise.contrib.test.condition import NotEQ
1111
from tortoise.exceptions import ConfigurationError, FieldError
1212
from tortoise.expressions import Case, Q, When
13-
from tortoise.functions import Count, Sum
13+
from tortoise.functions import Count, Lower, Sum
1414

1515

1616
class TestOrderBy(test.TestCase):
@@ -95,6 +95,16 @@ async def test_order_by_aggregation_reversed(self):
9595
)
9696
self.assertEqual([t.name for t in tournaments], ["1", "2"])
9797

98+
async def test_order_by_reserved_word_annotation(self):
99+
await Tournament.create(name="1")
100+
await Tournament.create(name="2")
101+
102+
reserved_words = ["order", "group", "limit", "offset", "where"]
103+
104+
for word in reserved_words:
105+
tournaments = await Tournament.annotate(**{word: Lower("name")}).order_by(word)
106+
self.assertEqual([t.name for t in tournaments], ["1", "2"])
107+
98108
async def test_distinct_values_with_annotation(self):
99109
await Tournament.create(name="3")
100110
await Tournament.create(name="1")
@@ -115,7 +125,7 @@ async def test_distinct_values_with_annotation(self):
115125
)
116126
self.assertEqual([t["name"] for t in tournaments], ["1", "2", "3"])
117127

118-
async def test_distinct_all_withl_annotation(self):
128+
async def test_distinct_all_with_annotation(self):
119129
await Tournament.create(name="3")
120130
await Tournament.create(name="1")
121131
await Tournament.create(name="2")

tortoise/queryset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pypika_tortoise.analytics import Count
1010
from pypika_tortoise.functions import Cast
1111
from pypika_tortoise.queries import QueryBuilder
12-
from pypika_tortoise.terms import Case, Field, PseudoColumn, Star, Term, ValueWrapper
12+
from pypika_tortoise.terms import Case, Field, Star, Term, ValueWrapper
1313
from typing_extensions import Literal, Protocol
1414

1515
from tortoise.backends.base.client import BaseDBAsyncClient, Capabilities
@@ -223,7 +223,7 @@ def resolve_ordering(
223223
# - Empty fields_for_select means that all columns and annotations are selected,
224224
# hence we can reference the annotation.
225225
# - The annotation is in fields_for_select, hence we can reference it.
226-
term = PseudoColumn(field_name)
226+
term = Field(field_name)
227227
else:
228228
# The annotation is not in SELECT, resolve it
229229
annotation = annotations[field_name]

0 commit comments

Comments
 (0)