-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1028 Document how to achieve a
OneToOneField
in Piccolo (#1029)
* add reverse method * one to one field docs * add FanClub to playground * first attempt at making reverse work on multiple levels * make reverse work multiple levels deep * remove unused import * more on to one to its own page * break up the one to one docs into sections * final tweaks to docs
- Loading branch information
1 parent
2335b95
commit 4be8aec
Showing
10 changed files
with
244 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
.. _OneToOne: | ||
|
||
One to One | ||
========== | ||
|
||
Schema | ||
------ | ||
|
||
A one to one relationship is basically just a foreign key with a unique | ||
constraint. In Piccolo, you can do it like this: | ||
|
||
.. code-block:: python | ||
from piccolo.table import Table | ||
from piccolo.columns import ForeignKey, Varchar, Text | ||
class Band(Table): | ||
name = Varchar() | ||
class FanClub(Table): | ||
band = ForeignKey(Band, unique=True) # <- Note the unique constraint | ||
address = Text() | ||
Queries | ||
------- | ||
|
||
Getting a related object | ||
~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
If we have a ``Band`` object: | ||
|
||
.. code-block:: python | ||
band = await Band.objects().where(Band.name == "Pythonistas").first() | ||
To get the associated ``FanClub`` object, you could do this: | ||
|
||
.. code-block:: python | ||
fan_club = await FanClub.objects().where(FanClub.band == band).first() | ||
Or alternatively, using ``get_related``: | ||
|
||
.. code-block:: python | ||
fan_club = await band.get_related(Band.id.join_on(FanClub.band)) | ||
Instead of using ``join_on``, you can use ``reverse`` to traverse the foreign | ||
key backwards if you prefer: | ||
|
||
.. code-block:: python | ||
fan_club = await band.get_related(FanClub.band.reverse()) | ||
Select | ||
~~~~~~ | ||
|
||
If doing a select query, and you want data from the related table: | ||
|
||
.. code-block:: python | ||
>>> await Band.select( | ||
... Band.name, | ||
... Band.id.join_on(FanClub.band).address.as_alias("address") | ||
... ) | ||
[{'name': 'Pythonistas', 'address': '1 Flying Circus, UK'}, ...] | ||
Where | ||
~~~~~ | ||
|
||
If you want to filter by related tables in the ``where`` clause: | ||
|
||
.. code-block:: python | ||
>>> await Band.select( | ||
... Band.name, | ||
... ).where(Band.id.join_on(FanClub.band).address.like("%Flying%")) | ||
[{'name': 'Pythonistas'}] | ||
Source | ||
------ | ||
|
||
.. currentmodule:: piccolo.columns.column_types | ||
|
||
.. automethod:: ForeignKey.reverse |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from piccolo.columns import ForeignKey, Text, Varchar | ||
from piccolo.table import Table | ||
from tests.base import TableTest | ||
|
||
|
||
class Band(Table): | ||
name = Varchar() | ||
|
||
|
||
class FanClub(Table): | ||
address = Text() | ||
band = ForeignKey(Band, unique=True) | ||
|
||
|
||
class Treasurer(Table): | ||
name = Varchar() | ||
fan_club = ForeignKey(FanClub, unique=True) | ||
|
||
|
||
class TestReverse(TableTest): | ||
tables = [Band, FanClub, Treasurer] | ||
|
||
def setUp(self): | ||
super().setUp() | ||
|
||
band = Band({Band.name: "Pythonistas"}) | ||
band.save().run_sync() | ||
|
||
fan_club = FanClub( | ||
{FanClub.band: band, FanClub.address: "1 Flying Circus, UK"} | ||
) | ||
fan_club.save().run_sync() | ||
|
||
treasurer = Treasurer( | ||
{Treasurer.fan_club: fan_club, Treasurer.name: "Bob"} | ||
) | ||
treasurer.save().run_sync() | ||
|
||
def test_reverse(self): | ||
response = Band.select( | ||
Band.name, | ||
FanClub.band.reverse().address.as_alias("address"), | ||
Treasurer.fan_club._.band.reverse().name.as_alias( | ||
"treasurer_name" | ||
), | ||
).run_sync() | ||
self.assertListEqual( | ||
response, | ||
[ | ||
{ | ||
"name": "Pythonistas", | ||
"address": "1 Flying Circus, UK", | ||
"treasurer_name": "Bob", | ||
} | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters