Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Changelog
Fixed
^^^^^
- Fix model with multi m2m fields generates wrong references name (#1897)
- Fix using reserved words in order_by (#1900)

0.24.1
------
Expand Down
14 changes: 12 additions & 2 deletions tests/test_order_by.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from tortoise.contrib.test.condition import NotEQ
from tortoise.exceptions import ConfigurationError, FieldError
from tortoise.expressions import Case, Q, When
from tortoise.functions import Count, Sum
from tortoise.functions import Count, Lower, Sum


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

async def test_order_by_reserved_word_annotation(self):
await Tournament.create(name="1")
await Tournament.create(name="2")

reserved_words = ["order", "group", "limit", "offset", "where"]

for word in reserved_words:
tournaments = await Tournament.annotate(**{word: Lower("name")}).order_by(word)
self.assertEqual([t.name for t in tournaments], ["1", "2"])

async def test_distinct_values_with_annotation(self):
await Tournament.create(name="3")
await Tournament.create(name="1")
Expand All @@ -115,7 +125,7 @@ async def test_distinct_values_with_annotation(self):
)
self.assertEqual([t["name"] for t in tournaments], ["1", "2", "3"])

async def test_distinct_all_withl_annotation(self):
async def test_distinct_all_with_annotation(self):
await Tournament.create(name="3")
await Tournament.create(name="1")
await Tournament.create(name="2")
Expand Down
4 changes: 2 additions & 2 deletions tortoise/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pypika_tortoise.analytics import Count
from pypika_tortoise.functions import Cast
from pypika_tortoise.queries import QueryBuilder
from pypika_tortoise.terms import Case, Field, PseudoColumn, Star, Term, ValueWrapper
from pypika_tortoise.terms import Case, Field, Star, Term, ValueWrapper
from typing_extensions import Literal, Protocol

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