piccolo 1.19.1__py3-none-any.whl → 1.20.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- piccolo/__init__.py +1 -1
- piccolo/query/methods/objects.py +58 -1
- piccolo/table.py +7 -18
- {piccolo-1.19.1.dist-info → piccolo-1.20.0.dist-info}/METADATA +1 -1
- {piccolo-1.19.1.dist-info → piccolo-1.20.0.dist-info}/RECORD +10 -10
- tests/table/instance/test_get_related.py +81 -25
- {piccolo-1.19.1.dist-info → piccolo-1.20.0.dist-info}/LICENSE +0 -0
- {piccolo-1.19.1.dist-info → piccolo-1.20.0.dist-info}/WHEEL +0 -0
- {piccolo-1.19.1.dist-info → piccolo-1.20.0.dist-info}/entry_points.txt +0 -0
- {piccolo-1.19.1.dist-info → piccolo-1.20.0.dist-info}/top_level.txt +0 -0
piccolo/__init__.py
CHANGED
@@ -1 +1 @@
|
|
1
|
-
__VERSION__ = "1.
|
1
|
+
__VERSION__ = "1.20.0"
|
piccolo/query/methods/objects.py
CHANGED
@@ -2,7 +2,7 @@ from __future__ import annotations
|
|
2
2
|
|
3
3
|
import typing as t
|
4
4
|
|
5
|
-
from piccolo.columns.column_types import ForeignKey
|
5
|
+
from piccolo.columns.column_types import ForeignKey, ReferencedTable
|
6
6
|
from piccolo.columns.combination import And, Where
|
7
7
|
from piccolo.custom_types import Combinable, TableInstance
|
8
8
|
from piccolo.engine.base import BaseBatch
|
@@ -231,6 +231,63 @@ class UpdateSelf:
|
|
231
231
|
return run_sync(self.run(*args, **kwargs))
|
232
232
|
|
233
233
|
|
234
|
+
class GetRelated(t.Generic[ReferencedTable]):
|
235
|
+
|
236
|
+
def __init__(self, row: Table, foreign_key: ForeignKey[ReferencedTable]):
|
237
|
+
self.row = row
|
238
|
+
self.foreign_key = foreign_key
|
239
|
+
|
240
|
+
async def run(
|
241
|
+
self,
|
242
|
+
node: t.Optional[str] = None,
|
243
|
+
in_pool: bool = True,
|
244
|
+
) -> t.Optional[ReferencedTable]:
|
245
|
+
if not self.row._exists_in_db:
|
246
|
+
raise ValueError("The object doesn't exist in the database.")
|
247
|
+
|
248
|
+
root_table = self.row.__class__
|
249
|
+
|
250
|
+
data = (
|
251
|
+
await root_table.select(
|
252
|
+
*[
|
253
|
+
i.as_alias(i._meta.name)
|
254
|
+
for i in self.foreign_key.all_columns()
|
255
|
+
]
|
256
|
+
)
|
257
|
+
.where(
|
258
|
+
root_table._meta.primary_key
|
259
|
+
== getattr(self.row, root_table._meta.primary_key._meta.name)
|
260
|
+
)
|
261
|
+
.first()
|
262
|
+
.run(node=node, in_pool=in_pool)
|
263
|
+
)
|
264
|
+
|
265
|
+
# Make sure that some values were returned:
|
266
|
+
if data is None or not any(data.values()):
|
267
|
+
return None
|
268
|
+
|
269
|
+
references = t.cast(
|
270
|
+
t.Type[ReferencedTable],
|
271
|
+
self.foreign_key._foreign_key_meta.resolved_references,
|
272
|
+
)
|
273
|
+
|
274
|
+
referenced_object = references(**data)
|
275
|
+
referenced_object._exists_in_db = True
|
276
|
+
return referenced_object
|
277
|
+
|
278
|
+
def __await__(
|
279
|
+
self,
|
280
|
+
) -> t.Generator[None, None, t.Optional[ReferencedTable]]:
|
281
|
+
"""
|
282
|
+
If the user doesn't explicity call .run(), proxy to it as a
|
283
|
+
convenience.
|
284
|
+
"""
|
285
|
+
return self.run().__await__()
|
286
|
+
|
287
|
+
def run_sync(self, *args, **kwargs) -> t.Optional[ReferencedTable]:
|
288
|
+
return run_sync(self.run(*args, **kwargs))
|
289
|
+
|
290
|
+
|
234
291
|
###############################################################################
|
235
292
|
|
236
293
|
|
piccolo/table.py
CHANGED
@@ -46,7 +46,7 @@ from piccolo.query import (
|
|
46
46
|
)
|
47
47
|
from piccolo.query.methods.create_index import CreateIndex
|
48
48
|
from piccolo.query.methods.indexes import Indexes
|
49
|
-
from piccolo.query.methods.objects import
|
49
|
+
from piccolo.query.methods.objects import GetRelated, UpdateSelf
|
50
50
|
from piccolo.query.methods.refresh import Refresh
|
51
51
|
from piccolo.querystring import QueryString
|
52
52
|
from piccolo.utils import _camel_to_snake
|
@@ -612,14 +612,14 @@ class Table(metaclass=TableMetaclass):
|
|
612
612
|
@t.overload
|
613
613
|
def get_related(
|
614
614
|
self, foreign_key: ForeignKey[ReferencedTable]
|
615
|
-
) ->
|
615
|
+
) -> GetRelated[ReferencedTable]: ...
|
616
616
|
|
617
617
|
@t.overload
|
618
|
-
def get_related(self, foreign_key: str) ->
|
618
|
+
def get_related(self, foreign_key: str) -> GetRelated[Table]: ...
|
619
619
|
|
620
620
|
def get_related(
|
621
621
|
self, foreign_key: t.Union[str, ForeignKey[ReferencedTable]]
|
622
|
-
) ->
|
622
|
+
) -> GetRelated[ReferencedTable]:
|
623
623
|
"""
|
624
624
|
Used to fetch a ``Table`` instance, for the target of a foreign key.
|
625
625
|
|
@@ -630,8 +630,8 @@ class Table(metaclass=TableMetaclass):
|
|
630
630
|
>>> print(manager.name)
|
631
631
|
'Guido'
|
632
632
|
|
633
|
-
It can only follow foreign keys
|
634
|
-
|
633
|
+
It can only follow foreign keys multiple levels deep. For example,
|
634
|
+
``Concert.band_1.manager``.
|
635
635
|
|
636
636
|
"""
|
637
637
|
if isinstance(foreign_key, str):
|
@@ -645,18 +645,7 @@ class Table(metaclass=TableMetaclass):
|
|
645
645
|
"ForeignKey column."
|
646
646
|
)
|
647
647
|
|
648
|
-
|
649
|
-
|
650
|
-
references = foreign_key._foreign_key_meta.resolved_references
|
651
|
-
|
652
|
-
return (
|
653
|
-
references.objects()
|
654
|
-
.where(
|
655
|
-
foreign_key._foreign_key_meta.resolved_target_column
|
656
|
-
== getattr(self, column_name)
|
657
|
-
)
|
658
|
-
.first()
|
659
|
-
)
|
648
|
+
return GetRelated(foreign_key=foreign_key, row=self)
|
660
649
|
|
661
650
|
def get_m2m(self, m2m: M2M) -> M2MGetRelated:
|
662
651
|
"""
|
@@ -1,10 +1,10 @@
|
|
1
|
-
piccolo/__init__.py,sha256=
|
1
|
+
piccolo/__init__.py,sha256=9C2I7AvMRyI69S3jlrRv3hQ4kYCgUZBXUC_VSvY3Dv0,23
|
2
2
|
piccolo/custom_types.py,sha256=7HMQAze-5mieNLfbQ5QgbRQgR2abR7ol0qehv2SqROY,604
|
3
3
|
piccolo/main.py,sha256=1VsFV67FWTUikPTysp64Fmgd9QBVa_9wcwKfwj2UCEA,5117
|
4
4
|
piccolo/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
5
5
|
piccolo/querystring.py,sha256=yZdURtiVSlxkkEVJoFKAmL2OfNxrUr8xfsfuBBB7IuY,9662
|
6
6
|
piccolo/schema.py,sha256=qNNy4tG_HqnXR9t3hHMgYXtGxHabwQAhUpc6RKLJ_gE,7960
|
7
|
-
piccolo/table.py,sha256=
|
7
|
+
piccolo/table.py,sha256=UvEbagMYRkTbyFHTUwUshZlL_dC4UKDP7vUOwF8OXmg,50593
|
8
8
|
piccolo/table_reflection.py,sha256=jrN1nHerDJ4tU09GtNN3hz7ap-7rXnSUjljFO6LB2H0,7094
|
9
9
|
piccolo/apps/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
10
10
|
piccolo/apps/app/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
@@ -166,7 +166,7 @@ piccolo/query/methods/drop_index.py,sha256=5x3vHpoOmQ1SMhj6L7snKXX6M9l9j1E1PFSO6
|
|
166
166
|
piccolo/query/methods/exists.py,sha256=lTMjtrFPFygZmaPV3sfQKXc3K0sVqJ2S6PDc3fRK6YQ,1203
|
167
167
|
piccolo/query/methods/indexes.py,sha256=J-QUqaBJwpgahskUH0Cu0Mq7zEKcfVAtDsUVIVX-C4c,943
|
168
168
|
piccolo/query/methods/insert.py,sha256=ssLJ_wn08KnOwwr7t-VILyn1P4hrvM63CfPIcAJWT5k,4701
|
169
|
-
piccolo/query/methods/objects.py,sha256=
|
169
|
+
piccolo/query/methods/objects.py,sha256=BfCOIbNMj7FWkmK5STaINkfDFmwzZvDZi60jCumjs1o,15627
|
170
170
|
piccolo/query/methods/raw.py,sha256=wQWR8b-yA_Gr-5lqRMZe9BOAAMBAw8CqTx37qVYvM1A,987
|
171
171
|
piccolo/query/methods/refresh.py,sha256=wg1zghKfwz-VmqK4uWa4GNMiDtK-skTqow591Hb3ONM,5854
|
172
172
|
piccolo/query/methods/select.py,sha256=jNR7CsEPUarffYMsXytKm7iScrQ_nqpRX-5mTPrXfjg,22414
|
@@ -346,7 +346,7 @@ tests/table/test_update.py,sha256=Cqi0xX3kEuJ0k-x_emPGB3arXuGWZ9e3CJ3HPFnw9Zw,20
|
|
346
346
|
tests/table/test_update_self.py,sha256=im6HcM-WLkEhZP0vTL42tYEJZyAZG6gDOxCnbikCBD4,907
|
347
347
|
tests/table/instance/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
348
348
|
tests/table/instance/test_create.py,sha256=JD0l7L9dDK1FKPhUs6WC_B2bruPR1qQ8aIqXpEbfiUg,1105
|
349
|
-
tests/table/instance/test_get_related.py,sha256=
|
349
|
+
tests/table/instance/test_get_related.py,sha256=eracFunh4Qlj5BEkI7OsrOyefRZM0rxrXnFX92VL1ZE,3285
|
350
350
|
tests/table/instance/test_get_related_readable.py,sha256=QDMMZykxPsTWcsl8ZIZtmQVLwSGCw7QBilLepAAAnWg,4694
|
351
351
|
tests/table/instance/test_instantiate.py,sha256=jvtaqSa_zN1lHQiykN4EnwitZqkWAbXle5IJtyhKuHY,958
|
352
352
|
tests/table/instance/test_remove.py,sha256=Zv22ZZqot61rjCVWL1PHDf1oxELcBnmMXx1gsST6j80,648
|
@@ -368,9 +368,9 @@ tests/utils/test_sql_values.py,sha256=vzxRmy16FfLZPH-sAQexBvsF9MXB8n4smr14qoEOS5
|
|
368
368
|
tests/utils/test_sync.py,sha256=9ytVo56y2vPQePvTeIi9lHIouEhWJbodl1TmzkGFrSo,799
|
369
369
|
tests/utils/test_table_reflection.py,sha256=SIzuat-IpcVj1GCFyOWKShI8YkhdOPPFH7qVrvfyPNE,3794
|
370
370
|
tests/utils/test_warnings.py,sha256=NvSC_cvJ6uZcwAGf1m-hLzETXCqprXELL8zg3TNLVMw,269
|
371
|
-
piccolo-1.
|
372
|
-
piccolo-1.
|
373
|
-
piccolo-1.
|
374
|
-
piccolo-1.
|
375
|
-
piccolo-1.
|
376
|
-
piccolo-1.
|
371
|
+
piccolo-1.20.0.dist-info/LICENSE,sha256=zFIpi-16uIJ420UMIG75NU0JbDBykvrdnXcj5U_EYBI,1059
|
372
|
+
piccolo-1.20.0.dist-info/METADATA,sha256=xOURp1sxZOGQ4Q_cnDb9pYcV5XuMlDGEOZ89k9KC5f0,5178
|
373
|
+
piccolo-1.20.0.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
|
374
|
+
piccolo-1.20.0.dist-info/entry_points.txt,sha256=SJPHET4Fi1bN5F3WqcKkv9SClK3_F1I7m4eQjk6AFh0,46
|
375
|
+
piccolo-1.20.0.dist-info/top_level.txt,sha256=-SR74VGbk43VoPy1HH-mHm97yoGukLK87HE5kdBW6qM,24
|
376
|
+
piccolo-1.20.0.dist-info/RECORD,,
|
@@ -1,42 +1,98 @@
|
|
1
1
|
import typing as t
|
2
|
-
from unittest import TestCase
|
3
2
|
|
4
|
-
from
|
3
|
+
from piccolo.testing.test_case import AsyncTableTest
|
4
|
+
from tests.example_apps.music.tables import Band, Concert, Manager, Venue
|
5
5
|
|
6
|
-
TABLES = [Manager, Band]
|
7
6
|
|
7
|
+
class TestGetRelated(AsyncTableTest):
|
8
|
+
tables = [Manager, Band, Concert, Venue]
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
for table in TABLES:
|
12
|
-
table.create_table().run_sync()
|
10
|
+
async def asyncSetUp(self):
|
11
|
+
await super().asyncSetUp()
|
13
12
|
|
14
|
-
|
15
|
-
|
16
|
-
table.alter().drop_table().run_sync()
|
13
|
+
# Setup two pairs of manager/band, so we can make sure the correct
|
14
|
+
# objects are returned.
|
17
15
|
|
18
|
-
|
16
|
+
self.manager = Manager(name="Guido")
|
17
|
+
await self.manager.save()
|
18
|
+
|
19
|
+
self.band = Band(
|
20
|
+
name="Pythonistas", manager=self.manager.id, popularity=100
|
21
|
+
)
|
22
|
+
await self.band.save()
|
23
|
+
|
24
|
+
self.manager_2 = Manager(name="Graydon")
|
25
|
+
await self.manager_2.save()
|
26
|
+
|
27
|
+
self.band_2 = Band(
|
28
|
+
name="Rustaceans", manager=self.manager_2.id, popularity=100
|
29
|
+
)
|
30
|
+
await self.band_2.save()
|
31
|
+
|
32
|
+
async def test_foreign_key(self) -> None:
|
19
33
|
"""
|
20
34
|
Make sure you can get a related object from another object instance.
|
21
35
|
"""
|
22
|
-
manager =
|
23
|
-
manager
|
36
|
+
manager = await self.band.get_related(Band.manager)
|
37
|
+
assert manager is not None
|
38
|
+
self.assertTrue(manager.id == self.manager.id)
|
39
|
+
|
40
|
+
manager_2 = await self.band_2.get_related(Band.manager)
|
41
|
+
assert manager_2 is not None
|
42
|
+
self.assertTrue(manager_2.id == self.manager_2.id)
|
24
43
|
|
25
|
-
|
26
|
-
|
44
|
+
async def test_non_foreign_key(self):
|
45
|
+
"""
|
46
|
+
Make sure that non-ForeignKey raise an exception.
|
47
|
+
"""
|
48
|
+
with self.assertRaises(ValueError):
|
49
|
+
self.band.get_related(Band.name) # type: ignore
|
27
50
|
|
28
|
-
|
29
|
-
|
30
|
-
|
51
|
+
async def test_string(self):
|
52
|
+
"""
|
53
|
+
Make sure it also works using a string representation of a foreign key.
|
54
|
+
"""
|
55
|
+
manager = t.cast(Manager, await self.band.get_related("manager"))
|
56
|
+
self.assertTrue(manager.id == self.manager.id)
|
31
57
|
|
32
|
-
|
58
|
+
async def test_invalid_string(self):
|
59
|
+
"""
|
60
|
+
Make sure an exception is raised if the foreign key string is invalid.
|
61
|
+
"""
|
33
62
|
with self.assertRaises(ValueError):
|
34
|
-
band.get_related(
|
63
|
+
self.band.get_related("abc123")
|
35
64
|
|
36
|
-
|
37
|
-
|
38
|
-
|
65
|
+
async def test_multiple_levels(self):
|
66
|
+
"""
|
67
|
+
Make sure ``get_related`` works multiple levels deep.
|
68
|
+
"""
|
69
|
+
concert = Concert(band_1=self.band, band_2=self.band_2)
|
70
|
+
await concert.save()
|
71
|
+
|
72
|
+
manager = await concert.get_related(Concert.band_1._.manager)
|
73
|
+
assert manager is not None
|
74
|
+
self.assertTrue(manager.id == self.manager.id)
|
75
|
+
|
76
|
+
manager_2 = await concert.get_related(Concert.band_2._.manager)
|
77
|
+
assert manager_2 is not None
|
78
|
+
self.assertTrue(manager_2.id == self.manager_2.id)
|
79
|
+
|
80
|
+
async def test_no_match(self):
|
81
|
+
"""
|
82
|
+
If not related object exists, make sure ``None`` is returned.
|
83
|
+
"""
|
84
|
+
concert = Concert(band_1=self.band, band_2=None)
|
85
|
+
await concert.save()
|
86
|
+
|
87
|
+
manager_2 = await concert.get_related(Concert.band_2._.manager)
|
88
|
+
assert manager_2 is None
|
89
|
+
|
90
|
+
async def test_not_in_db(self):
|
91
|
+
"""
|
92
|
+
If the object we're calling ``get_related`` on doesn't exist in the
|
93
|
+
database, then make sure an error is raised.
|
94
|
+
"""
|
95
|
+
concert = Concert(band_1=self.band, band_2=self.band_2)
|
39
96
|
|
40
|
-
# Test an invalid string
|
41
97
|
with self.assertRaises(ValueError):
|
42
|
-
|
98
|
+
await concert.get_related(Concert.band_1._.manager)
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|