Skip to content

Commit

Permalink
add option to change default order in results
Browse files Browse the repository at this point in the history
  • Loading branch information
sinisaos committed Dec 13, 2024
1 parent 70b183a commit bda0815
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 4 deletions.
27 changes: 23 additions & 4 deletions docs/src/piccolo/schema/reverse_lookup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ We create it in Piccolo like this:
class Manager(Table):
name = Varchar()
bands = ReverseLookup(
LazyTableReference(
"Band",
module_path=__name__,
),
LazyTableReference("Band", module_path=__name__),
reverse_fk="manager",
)
Expand Down Expand Up @@ -102,4 +99,26 @@ If you omit the columns argument, then all of the columns are returned.
{'id': 2, 'name': 'Rustaceans'},
]
}
]
The default order of reverse lookup results is ascending, but if you
specify ``descending=True``, you can get the results in descending order.

.. code-block:: python
>>> await Manager.select(Manager.name, Manager.bands(descending=True))
[
{
'name': 'John',
'bands': [
{'id': 3, 'name': 'C-Sharps'},
]
},
{
'name': 'Guido',
'bands': [
{'id': 2, 'name': 'Rustaceans'},
{'id': 1, 'name': 'Pythonistas'},
]
}
]
10 changes: 10 additions & 0 deletions piccolo/columns/reverse_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(
reverse_lookup: ReverseLookup,
as_list: bool = False,
load_json: bool = False,
descending: bool = False,
):
"""
:param columns:
Expand All @@ -36,12 +37,16 @@ def __init__(
flattened list will be returned, rather than a list of objects.
:param load_json:
If ``True``, any JSON strings are loaded as Python objects.
:param descending:
If ``True'', reverse lookup results sorted in descending order,
otherwise in default ascending order.
"""
self.as_list = as_list
self.columns = columns
self.reverse_lookup = reverse_lookup
self.load_json = load_json
self.descending = descending

safe_types = [int, str]

Expand Down Expand Up @@ -210,6 +215,7 @@ def __call__(
*columns: Column,
as_list: bool = False,
load_json: bool = False,
descending: bool = False,
) -> ReverseLookupSelect:
"""
:param columns:
Expand All @@ -220,6 +226,9 @@ def __call__(
flattened list will be returned, rather than a list of objects.
:param load_json:
If ``True``, any JSON strings are loaded as Python objects.
:param descending:
If ``True'', reverse lookup results sorted in descending order,
otherwise in default ascending order.
"""

Expand All @@ -233,4 +242,5 @@ def __call__(
reverse_lookup=self,
as_list=as_list,
load_json=load_json,
descending=descending,
)
6 changes: 6 additions & 0 deletions piccolo/query/methods/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,12 @@ async def response_handler(self, response: t.List[t.Dict[str, t.Any]]):
as_list=False,
)

if reverse_lookup_select.descending:
for row in response:
row[reverse_lookup_name] = list(
reversed(row[reverse_lookup_name])
)

#######################################################################

# If no columns were specified, it's a select *, so we know that
Expand Down
64 changes: 64 additions & 0 deletions tests/columns/test_reverse_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,54 @@ def test_select_id(self):
],
)

@engines_skip("cockroach")
def test_select_multiple_all_columns_descending(self):
"""
🐛 Cockroach bug: https://github.com/cockroachdb/cockroach/issues/71908 "could not decorrelate subquery" error under asyncpg
""" # noqa: E501
response = Manager.select(
Manager.name, Manager.bands(descending=True)
).run_sync()

self.assertEqual(
response,
[
{
"name": "Guido",
"bands": [
{"id": 2, "name": "Rustaceans", "manager": 1},
{"id": 1, "name": "Pythonistas", "manager": 1},
],
},
{
"name": "Mark",
"bands": [{"id": 3, "name": "C-Sharps", "manager": 2}],
},
{
"name": "John",
"bands": [],
},
],
)

@engines_skip("cockroach")
def test_select_id_descending(self):
"""
🐛 Cockroach bug: https://github.com/cockroachdb/cockroach/issues/71908 "could not decorrelate subquery" error under asyncpg
""" # noqa: E501
response = Manager.select(
Manager.name, Manager.bands(Band.id, as_list=True, descending=True)
).run_sync()

self.assertEqual(
response,
[
{"name": "Guido", "bands": [2, 1]},
{"name": "Mark", "bands": [3]},
{"name": "John", "bands": []},
],
)

def test_select_multiple_as_list_error(self):

with self.assertRaises(ValueError):
Expand Down Expand Up @@ -265,3 +313,19 @@ def test_select_custom_primary_key(self):
{"name": "Fred", "concerts": []},
],
)

response = Customer.select(
Customer.name, Customer.concerts(Concert.name, descending=True)
).run_sync()

self.assertEqual(
response,
[
{
"name": "Bob",
"concerts": [{"name": "Folkfest"}, {"name": "Rockfest"}],
},
{"name": "Sally", "concerts": [{"name": "Classicfest"}]},
{"name": "Fred", "concerts": []},
],
)

0 comments on commit bda0815

Please sign in to comment.