Skip to content

Commit

Permalink
migrations can now handle ForeignKey lazy imports
Browse files Browse the repository at this point in the history
  • Loading branch information
dantownsend committed Jan 1, 2021
1 parent 95aba82 commit 1c6c00a
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion piccolo/apps/migrations/auto/serialisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import uuid

from piccolo.columns.defaults.base import Default
from piccolo.columns.reference import LazyTableReference
from piccolo.table import Table
from .serialisation_legacy import deserialise_legacy_params

Expand All @@ -25,9 +26,15 @@ def __hash__(self):
def __eq__(self, other):
return self.__hash__() == other.__hash__()

def _serialise_value(self, value):
return f"'{value}'" if isinstance(value, str) else value

def __repr__(self):
args = ", ".join(
[f"{key}={value}" for key, value in self.instance.__dict__.items()]
[
f"{key}={self._serialise_value(value)}"
for key, value in self.instance.__dict__.items()
]
)
return f"{self.instance.__class__.__name__}({args})"

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

# Lazy imports - we need to resolve these now, in case the target
# table class gets deleted in the future.
if isinstance(value, LazyTableReference):
table_type = value.resolve()
params[key] = SerialisedCallable(callable_=table_type)
extra_definitions.append(
SerialisedTableType(table_type=table_type)
)
extra_imports.append(
Import(module=Table.__module__, target="Table")
)
continue

# Replace any Table class values into class and table names
if inspect.isclass(value) and issubclass(value, Table):
params[key] = SerialisedCallable(callable_=value)
Expand Down

0 comments on commit 1c6c00a

Please sign in to comment.