Skip to content

BaseField class

saffier.core.db.fields.base.Field

Field(*, primary_key=False, index=False, unique=False, **kwargs)

Base field for the model declaration fields.

PARAMETER DESCRIPTION
primary_key

TYPE: bool DEFAULT: False

index

TYPE: bool DEFAULT: False

unique

TYPE: bool DEFAULT: False

**kwargs

TYPE: Any DEFAULT: {}

Source code in saffier/core/db/fields/base.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def __init__(
    self,
    *,
    primary_key: bool = False,
    index: bool = False,
    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, server_default=self.server_default)
        kwargs["read_only"] = True

    self.null = kwargs.get("null", False)
    self.default_value = kwargs.get("default", None)
    self.primary_key = primary_key
    self.index = index
    self.unique = unique
    self.validator: typing.Union[SaffierField, typing.Type[SaffierField]] = self.get_validator(
        **kwargs
    )
    self.comment = kwargs.get("comment", None)
    self.owner = kwargs.pop("owner", None)
    self.server_onupdate = kwargs.pop("server_onupdate", None)
    self.autoincrement = kwargs.pop("autoincrement", False)
    self.secret = kwargs.pop("secret", False)

server_default instance-attribute

server_default = pop('server_default', None)

null instance-attribute

null = get('null', False)

default_value instance-attribute

default_value = get('default', None)

primary_key instance-attribute

primary_key = primary_key

index instance-attribute

index = index

unique instance-attribute

unique = unique

validator instance-attribute

validator = get_validator(**kwargs)

comment instance-attribute

comment = get('comment', None)

owner instance-attribute

owner = pop('owner', None)

server_onupdate instance-attribute

server_onupdate = pop('server_onupdate', None)

autoincrement instance-attribute

autoincrement = pop('autoincrement', False)

secret instance-attribute

secret = pop('secret', False)

get_column

get_column(name)

Returns a column type for the model field.

PARAMETER DESCRIPTION
name

TYPE: str

Source code in saffier/core/db/fields/base.py
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def get_column(self, name: str) -> sqlalchemy.Column:
    """
    Returns a column type for the model field.
    """
    column_type = self.get_column_type()
    constraints = self.get_constraints()

    return sqlalchemy.Column(
        name,
        column_type,
        *constraints,
        primary_key=self.primary_key,
        nullable=self.null and not self.primary_key,
        index=self.index,
        unique=self.unique,
        default=self.default_value,
        comment=self.comment,
        server_default=self.server_default,
    )

get_validator

get_validator(**kwargs)
PARAMETER DESCRIPTION
**kwargs

TYPE: Any DEFAULT: {}

Source code in saffier/core/db/fields/base.py
88
89
def get_validator(self, **kwargs: typing.Any) -> SaffierField:
    return SaffierField(**kwargs)  # pragma: no cover

get_column_type

get_column_type()
Source code in saffier/core/db/fields/base.py
91
92
def get_column_type(self) -> sqlalchemy.types.TypeEngine:
    raise NotImplementedError()  # pragma: no cover

get_constraints

get_constraints()
Source code in saffier/core/db/fields/base.py
94
95
def get_constraints(self) -> typing.Any:
    return []

expand_relationship

expand_relationship(value)
PARAMETER DESCRIPTION
value

TYPE: Any

Source code in saffier/core/db/fields/base.py
97
98
def expand_relationship(self, value: typing.Any) -> typing.Any:
    return value

raise_for_non_default

raise_for_non_default(default, server_default)
PARAMETER DESCRIPTION
default

TYPE: Any

server_default

TYPE: Any

Source code in saffier/core/db/fields/base.py
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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."
        )