Skip to content

Commit

Permalink
Fix server_default and update requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
tarsil committed Nov 30, 2023
1 parent 22ba64e commit 6736cf7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ dependencies = [
"anyio>=3.6.2,<5",
"click>=8.1.3,<9.0.0",
"loguru>=0.6.0,<0.10.0",
"databasez>=0.5.0",
"databasez>=0.7.0",
"orjson >=3.8.5,<4.0.0",
"pydantic>=2.5.2,<3.0.0",
"pydantic-settings>=2.0.0,<3.0.0",
Expand Down
20 changes: 16 additions & 4 deletions saffier/core/db/fields/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ def __init__(
unique: bool = False,
**kwargs: typing.Any,
) -> None:
self.server_default = kwargs.pop("server_default", None)
if primary_key:
default_value = kwargs.get("default", None)
self.raise_for_non_default(default=default_value)
self.raise_for_non_default(default=default_value, server_default=self.server_default)
kwargs["read_only"] = True

self.null = kwargs.get("null", False)
Expand All @@ -56,7 +57,6 @@ def __init__(
)
self.comment = kwargs.get("comment", None)
self.owner = kwargs.pop("owner", None)
self.server_default = kwargs.pop("server_default", None)
self.server_onupdate = kwargs.pop("server_onupdate", None)
self.autoincrement = kwargs.pop("autoincrement", False)

Expand Down Expand Up @@ -92,8 +92,20 @@ def get_constraints(self) -> typing.Any:
def expand_relationship(self, value: typing.Any) -> typing.Any:
return value

def raise_for_non_default(self, default: typing.Any) -> typing.Any:
if not isinstance(self, (IntegerField, BigIntegerField)) and not default:
def raise_for_non_default(self, default: typing.Any, server_default: typing.Any) -> typing.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 isinstance(self, (IntegerField, BigIntegerField))
and not has_default
and not has_server_default
):
raise ValueError(
"Primary keys other then IntegerField and BigIntegerField, must provide a default or a server_default."
)
Expand Down

0 comments on commit 6736cf7

Please sign in to comment.