Skip to content

Commit 1c6c00a

Browse files
committed
migrations can now handle ForeignKey lazy imports
1 parent 95aba82 commit 1c6c00a

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

piccolo/apps/migrations/auto/serialisation.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import uuid
1010

1111
from piccolo.columns.defaults.base import Default
12+
from piccolo.columns.reference import LazyTableReference
1213
from piccolo.table import Table
1314
from .serialisation_legacy import deserialise_legacy_params
1415

@@ -25,9 +26,15 @@ def __hash__(self):
2526
def __eq__(self, other):
2627
return self.__hash__() == other.__hash__()
2728

29+
def _serialise_value(self, value):
30+
return f"'{value}'" if isinstance(value, str) else value
31+
2832
def __repr__(self):
2933
args = ", ".join(
30-
[f"{key}={value}" for key, value in self.instance.__dict__.items()]
34+
[
35+
f"{key}={self._serialise_value(value)}"
36+
for key, value in self.instance.__dict__.items()
37+
]
3138
)
3239
return f"{self.instance.__class__.__name__}({args})"
3340

@@ -177,6 +184,19 @@ def serialise_params(params: t.Dict[str, t.Any]) -> SerialisedParams:
177184
)
178185
continue
179186

187+
# Lazy imports - we need to resolve these now, in case the target
188+
# table class gets deleted in the future.
189+
if isinstance(value, LazyTableReference):
190+
table_type = value.resolve()
191+
params[key] = SerialisedCallable(callable_=table_type)
192+
extra_definitions.append(
193+
SerialisedTableType(table_type=table_type)
194+
)
195+
extra_imports.append(
196+
Import(module=Table.__module__, target="Table")
197+
)
198+
continue
199+
180200
# Replace any Table class values into class and table names
181201
if inspect.isclass(value) and issubclass(value, Table):
182202
params[key] = SerialisedCallable(callable_=value)

0 commit comments

Comments
 (0)