Skip to content

Commit

Permalink
Merge pull request #47 from tarsil/fix/primary_keys
Browse files Browse the repository at this point in the history
Add server_default to non_default validation
  • Loading branch information
tarsil authored Nov 24, 2023
2 parents 9ef2324 + bfd5bc7 commit d1a8fd4
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions edgy/core/db/fields/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init__(
) -> None:
self.max_digits: str = kwargs.pop("max_digits", None)
self.decimal_places: str = kwargs.pop("decimal_places", None)
self.server_default: Any = kwargs.pop("server_default", None)

super().__init__(**kwargs)

Expand All @@ -37,7 +38,11 @@ def __init__(
default = None
if default is not Undefined:
self.default = default
if default is not None:
if (
(default is not None)
and default != Undefined
or (self.server_default is not None and self.server_default != Undefined)
):
self.null = True

self.defaulf_factory: Optional[Callable[..., Any]] = kwargs.pop(
Expand Down Expand Up @@ -75,14 +80,13 @@ def __init__(
"multiple_of", None
)
self.through: Any = kwargs.pop("through", None)
self.server_default: Any = kwargs.pop("server_default", None)
self.server_onupdate: Any = kwargs.pop("server_onupdate", None)
self.registry: Registry = kwargs.pop("registry", None)
self.comment = kwargs.pop("comment", None)

if self.primary_key:
default_value = default
self.raise_for_non_default(default=default_value)
self.raise_for_non_default(default=default_value, server_default=self.server_default)

for name, value in kwargs.items():
edgy_setattr(self, name, value)
Expand All @@ -109,8 +113,16 @@ def is_required(self) -> bool:
required = False if self.null else True
return bool(required and not self.primary_key)

def raise_for_non_default(self, default: Any) -> Any:
if not self.field_type == int and not default:
def raise_for_non_default(self, default: Any, server_default: Any) -> Any:
has_default: bool = True
has_server_default: bool = True

if default is None or default is False:
has_default = False
if server_default is None or server_default is False:
has_server_default = False

if not self.field_type == int and not has_default and not has_server_default:
raise FieldDefinitionError(
"Primary keys other then IntegerField and BigIntegerField, must provide a default or a server_default."
)
Expand Down

0 comments on commit d1a8fd4

Please sign in to comment.