Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix booleanfield logic during migrations #302

Merged
merged 2 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ hide:
### Fixed

- `hash_names` with unique_together incorrectly fed the `uc` prefix into the hasher. This makes the names unidentificatable.
- `BooleanField` logic for automic migrations works by using server_default.

## 0.28.0

Expand Down
16 changes: 11 additions & 5 deletions edgy/core/db/fields/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,19 +260,25 @@ class BooleanField(FieldFactory, bool_type):
def __new__( # type: ignore
cls,
*,
default: Optional[bool] = False,
default: Union[None, bool, Callable[[], bool]] = False,
**kwargs: Any,
) -> BaseFieldType:
kwargs = {
**kwargs,
**{key: value for key, value in locals().items() if key not in CLASS_DEFAULTS},
}
if default is not None:
kwargs["default"] = default
return super().__new__(cls, **kwargs)

@classmethod
def get_column_type(cls, kwargs: dict[str, Any]) -> Any:
return sqlalchemy.Boolean()

@classmethod
def validate(cls, kwargs: dict[str, Any]) -> None:
super().validate(kwargs)

default = kwargs.get("default")
if default is not None and isinstance(default, bool):
kwargs.setdefault("server_default", sqlalchemy.text("true" if default else "false"))


class DateTimeField(_AutoNowMixin, datetime.datetime):
"""Representation of a datetime field"""
Expand Down
2 changes: 2 additions & 0 deletions tests/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class User(edgy.StrictModel):
# simple default
active = edgy.fields.BooleanField(server_default=sqlalchemy.text("true"), default=False)
profile = edgy.fields.ForeignKey("Profile", null=False, default=complex_default)
# auto server defaults
is_staff = edgy.fields.BooleanField()

class Meta:
registry = models
Expand Down
2 changes: 2 additions & 0 deletions tests/cli/test_nullable_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ async def main():
async with main.models:
user = await main.User.query.get(name="edgy")
assert user.active
assert not user.is_staff
assert user.content_type.name == "User"
assert user.profile == await main.Profile.query.get(name="edgy")
assert user.profile.content_type.name == "Profile"
Expand All @@ -211,6 +212,7 @@ async def main():
async with main.models:
user = await main.User.query.get(name="edgy")
assert user.active
assert not user.is_staff
assert user.content_type.name == "User"
assert user.profile == await main.Profile.query.get(name="edgy")

Expand Down