Skip to content

Commit 79537ec

Browse files
committed
added docs for lazy table references in ForeignKey columns
1 parent 8f6e199 commit 79537ec

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

piccolo/columns/column_types.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,11 @@ class Band(Table):
904904
:param references:
905905
The ``Table`` being referenced.
906906
907+
.. code-block:: python
908+
909+
class Band(Table):
910+
manager = ForeignKey(references=Manager)
911+
907912
A table can have a reference to itself, if you pass a ``references``
908913
argument of ``'self'``.
909914
@@ -913,6 +918,44 @@ class Musician(Table):
913918
name = Varchar(length=100)
914919
instructor = ForeignKey(references='self')
915920
921+
In certain situations, you may be unable to reference a ``Table`` class
922+
if it causes a circular dependency. Try and avoid these by refactoring
923+
your code. If unavoidable, you can specify a lazy reference. If the
924+
``Table`` is defined in the same file:
925+
926+
.. code-block:: python
927+
928+
class Band(Table):
929+
manager = ForeignKey(references='Manager')
930+
931+
If the ``Table`` is defined in a Piccolo app:
932+
933+
.. code-block:: python
934+
935+
from piccolo.columns.reference import LazyTableReference
936+
937+
class Band(Table):
938+
manager = ForeignKey(
939+
references=LazyTableReference(
940+
table_class_name="Manager", app_name="my_app",
941+
)
942+
)
943+
944+
If you aren't using Piccolo apps, you can specify a ``Table`` in any
945+
Python module:
946+
947+
.. code-block:: python
948+
949+
from piccolo.columns.reference import LazyTableReference
950+
951+
class Band(Table):
952+
manager = ForeignKey(
953+
references=LazyTableReference(
954+
table_class_name="Manager",
955+
module_path="some_module.tables",
956+
)
957+
)
958+
916959
:param on_delete:
917960
Determines what the database should do when a row is deleted with
918961
foreign keys referencing it. If set to ``OnDelete.cascade``, any rows

0 commit comments

Comments
 (0)