Fields & Types¶
Fields are declared as type annotations on a Model, with Field(...) supplying database options (primary key, unique, index, defaults). The lower-level types on this page — FerroField, DbType, and friends — power Annotated[...] declarations and explicit column-type overrides.
Field(default=PydanticUndefined, *, primary_key=_Unset, autoincrement=_Unset, unique=_Unset, index=_Unset, back_ref=_Unset, many_to_many=_Unset, related_name=_Unset, through=_Unset, reverse_index=_Unset, nullable=_Unset, db_type=_Unset, db_check=_Unset, default_factory=_Unset, alias=_Unset, alias_priority=_Unset, validation_alias=_Unset, serialization_alias=_Unset, title=_Unset, field_title_generator=_Unset, description=_Unset, examples=_Unset, exclude=_Unset, exclude_if=_Unset, discriminator=_Unset, deprecated=_Unset, json_schema_extra=_Unset, frozen=_Unset, validate_default=_Unset, repr=_Unset, init=_Unset, init_var=_Unset, kw_only=_Unset, pattern=_Unset, strict=_Unset, coerce_numbers_to_str=_Unset, gt=_Unset, ge=_Unset, lt=_Unset, le=_Unset, multiple_of=_Unset, allow_inf_nan=_Unset, max_digits=_Unset, decimal_places=_Unset, min_length=_Unset, max_length=_Unset, union_mode=_Unset, fail_fast=_Unset, **extra)
¶
Field(default: Literal[Ellipsis], *, primary_key: bool = ..., autoincrement: bool | None = ..., unique: bool = ..., index: bool = ..., back_ref: bool = ..., many_to_many: bool = ..., related_name: str | None = ..., through: str | None = ..., nullable: FerroNullable = ..., db_type: DbType | None = ..., db_check: bool = ..., alias: str | None = ..., alias_priority: int | None = ..., validation_alias: str | AliasPath | AliasChoices | None = ..., serialization_alias: str | None = ..., title: str | None = ..., field_title_generator: Callable[[str, FieldInfo], str] | None = ..., description: str | None = ..., examples: list[Any] | None = ..., exclude: bool | None = ..., exclude_if: Callable[[Any], bool] | None = ..., discriminator: str | types.Discriminator | None = ..., deprecated: Deprecated | str | bool | None = ..., json_schema_extra: JsonDict | Callable[[JsonDict], None] | None = ..., frozen: bool | None = ..., validate_default: bool | None = ..., repr: bool = ..., init: bool | None = ..., init_var: bool | None = ..., kw_only: bool | None = ..., pattern: str | re.Pattern[str] | None = ..., strict: bool | None = ..., coerce_numbers_to_str: bool | None = ..., gt: annotated_types.SupportsGt | None = ..., ge: annotated_types.SupportsGe | None = ..., lt: annotated_types.SupportsLt | None = ..., le: annotated_types.SupportsLe | None = ..., multiple_of: float | None = ..., allow_inf_nan: bool | None = ..., max_digits: int | None = ..., decimal_places: int | None = ..., min_length: int | None = ..., max_length: int | None = ..., union_mode: Literal['smart', 'left_to_right'] = ..., fail_fast: bool | None = ..., **extra: Any) -> Any
Field(default: Any, *, primary_key: bool = ..., autoincrement: bool | None = ..., unique: bool = ..., index: bool = ..., back_ref: bool = ..., many_to_many: bool = ..., related_name: str | None = ..., through: str | None = ..., nullable: FerroNullable = ..., db_type: DbType | None = ..., db_check: bool = ..., alias: str | None = ..., alias_priority: int | None = ..., validation_alias: str | AliasPath | AliasChoices | None = ..., serialization_alias: str | None = ..., title: str | None = ..., field_title_generator: Callable[[str, FieldInfo], str] | None = ..., description: str | None = ..., examples: list[Any] | None = ..., exclude: bool | None = ..., exclude_if: Callable[[Any], bool] | None = ..., discriminator: str | types.Discriminator | None = ..., deprecated: Deprecated | str | bool | None = ..., json_schema_extra: JsonDict | Callable[[JsonDict], None] | None = ..., frozen: bool | None = ..., validate_default: Literal[True], repr: bool = ..., init: bool | None = ..., init_var: bool | None = ..., kw_only: bool | None = ..., pattern: str | re.Pattern[str] | None = ..., strict: bool | None = ..., coerce_numbers_to_str: bool | None = ..., gt: annotated_types.SupportsGt | None = ..., ge: annotated_types.SupportsGe | None = ..., lt: annotated_types.SupportsLt | None = ..., le: annotated_types.SupportsLe | None = ..., multiple_of: float | None = ..., allow_inf_nan: bool | None = ..., max_digits: int | None = ..., decimal_places: int | None = ..., min_length: int | None = ..., max_length: int | None = ..., union_mode: Literal['smart', 'left_to_right'] = ..., fail_fast: bool | None = ..., **extra: Any) -> Any
Field(default: _T, *, primary_key: bool = ..., autoincrement: bool | None = ..., unique: bool = ..., index: bool = ..., back_ref: bool = ..., many_to_many: bool = ..., related_name: str | None = ..., through: str | None = ..., nullable: FerroNullable = ..., db_type: DbType | None = ..., db_check: bool = ..., alias: str | None = ..., alias_priority: int | None = ..., validation_alias: str | AliasPath | AliasChoices | None = ..., serialization_alias: str | None = ..., title: str | None = ..., field_title_generator: Callable[[str, FieldInfo], str] | None = ..., description: str | None = ..., examples: list[Any] | None = ..., exclude: bool | None = ..., exclude_if: Callable[[Any], bool] | None = ..., discriminator: str | types.Discriminator | None = ..., deprecated: Deprecated | str | bool | None = ..., json_schema_extra: JsonDict | Callable[[JsonDict], None] | None = ..., frozen: bool | None = ..., validate_default: Literal[False] = ..., repr: bool = ..., init: bool | None = ..., init_var: bool | None = ..., kw_only: bool | None = ..., pattern: str | re.Pattern[str] | None = ..., strict: bool | None = ..., coerce_numbers_to_str: bool | None = ..., gt: annotated_types.SupportsGt | None = ..., ge: annotated_types.SupportsGe | None = ..., lt: annotated_types.SupportsLt | None = ..., le: annotated_types.SupportsLe | None = ..., multiple_of: float | None = ..., allow_inf_nan: bool | None = ..., max_digits: int | None = ..., decimal_places: int | None = ..., min_length: int | None = ..., max_length: int | None = ..., union_mode: Literal['smart', 'left_to_right'] = ..., fail_fast: bool | None = ..., **extra: Any) -> _T
Field(*, primary_key: bool = ..., autoincrement: bool | None = ..., unique: bool = ..., index: bool = ..., back_ref: bool = ..., many_to_many: bool = ..., related_name: str | None = ..., through: str | None = ..., nullable: FerroNullable = ..., db_type: DbType | None = ..., db_check: bool = ..., default_factory: Callable[[], Any] | Callable[[dict[str, Any]], Any], alias: str | None = ..., alias_priority: int | None = ..., validation_alias: str | AliasPath | AliasChoices | None = ..., serialization_alias: str | None = ..., title: str | None = ..., field_title_generator: Callable[[str, FieldInfo], str] | None = ..., description: str | None = ..., examples: list[Any] | None = ..., exclude: bool | None = ..., exclude_if: Callable[[Any], bool] | None = ..., discriminator: str | types.Discriminator | None = ..., deprecated: Deprecated | str | bool | None = ..., json_schema_extra: JsonDict | Callable[[JsonDict], None] | None = ..., frozen: bool | None = ..., validate_default: Literal[True], repr: bool = ..., init: bool | None = ..., init_var: bool | None = ..., kw_only: bool | None = ..., pattern: str | re.Pattern[str] | None = ..., strict: bool | None = ..., coerce_numbers_to_str: bool | None = ..., gt: annotated_types.SupportsGt | None = ..., ge: annotated_types.SupportsGe | None = ..., lt: annotated_types.SupportsLt | None = ..., le: annotated_types.SupportsLe | None = ..., multiple_of: float | None = ..., allow_inf_nan: bool | None = ..., max_digits: int | None = ..., decimal_places: int | None = ..., min_length: int | None = ..., max_length: int | None = ..., union_mode: Literal['smart', 'left_to_right'] = ..., fail_fast: bool | None = ..., **extra: Any) -> Any
Field(*, primary_key: bool = ..., autoincrement: bool | None = ..., unique: bool = ..., index: bool = ..., back_ref: bool = ..., many_to_many: bool = ..., related_name: str | None = ..., through: str | None = ..., nullable: FerroNullable = ..., db_type: DbType | None = ..., db_check: bool = ..., default_factory: Callable[[], _T] | Callable[[dict[str, Any]], _T], alias: str | None = ..., alias_priority: int | None = ..., validation_alias: str | AliasPath | AliasChoices | None = ..., serialization_alias: str | None = ..., title: str | None = ..., field_title_generator: Callable[[str, FieldInfo], str] | None = ..., description: str | None = ..., examples: list[Any] | None = ..., exclude: bool | None = ..., exclude_if: Callable[[Any], bool] | None = ..., discriminator: str | types.Discriminator | None = ..., deprecated: Deprecated | str | bool | None = ..., json_schema_extra: JsonDict | Callable[[JsonDict], None] | None = ..., frozen: bool | None = ..., validate_default: Literal[False] | None = ..., repr: bool = ..., init: bool | None = ..., init_var: bool | None = ..., kw_only: bool | None = ..., pattern: str | re.Pattern[str] | None = ..., strict: bool | None = ..., coerce_numbers_to_str: bool | None = ..., gt: annotated_types.SupportsGt | None = ..., ge: annotated_types.SupportsGe | None = ..., lt: annotated_types.SupportsLt | None = ..., le: annotated_types.SupportsLe | None = ..., multiple_of: float | None = ..., allow_inf_nan: bool | None = ..., max_digits: int | None = ..., decimal_places: int | None = ..., min_length: int | None = ..., max_length: int | None = ..., union_mode: Literal['smart', 'left_to_right'] = ..., fail_fast: bool | None = ..., **extra: Any) -> _T
Field(*, primary_key: bool = ..., autoincrement: bool | None = ..., unique: bool = ..., index: bool = ..., back_ref: bool = ..., many_to_many: bool = ..., related_name: str | None = ..., through: str | None = ..., nullable: FerroNullable = ..., db_type: DbType | None = ..., db_check: bool = ..., alias: str | None = ..., alias_priority: int | None = ..., validation_alias: str | AliasPath | AliasChoices | None = ..., serialization_alias: str | None = ..., title: str | None = ..., field_title_generator: Callable[[str, FieldInfo], str] | None = ..., description: str | None = ..., examples: list[Any] | None = ..., exclude: bool | None = ..., exclude_if: Callable[[Any], bool] | None = ..., discriminator: str | types.Discriminator | None = ..., deprecated: Deprecated | str | bool | None = ..., json_schema_extra: JsonDict | Callable[[JsonDict], None] | None = ..., frozen: bool | None = ..., validate_default: bool | None = ..., repr: bool = ..., init: bool | None = ..., init_var: bool | None = ..., kw_only: bool | None = ..., pattern: str | re.Pattern[str] | None = ..., strict: bool | None = ..., coerce_numbers_to_str: bool | None = ..., gt: annotated_types.SupportsGt | None = ..., ge: annotated_types.SupportsGe | None = ..., lt: annotated_types.SupportsLt | None = ..., le: annotated_types.SupportsLe | None = ..., multiple_of: float | None = ..., allow_inf_nan: bool | None = ..., max_digits: int | None = ..., decimal_places: int | None = ..., min_length: int | None = ..., max_length: int | None = ..., union_mode: Literal['smart', 'left_to_right'] = ..., fail_fast: bool | None = ..., **extra: Any) -> Any
Build field metadata with Pydantic and Ferro options
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default
|
Any
|
Default value used when the field is not set. |
PydanticUndefined
|
primary_key
|
bool | Any
|
Mark this column as the table primary key in Ferro. |
_Unset
|
autoincrement
|
bool | None | Any
|
Override automatic increment behavior for primary key columns. When not provided, Ferro infers this for integer primary keys. |
_Unset
|
unique
|
bool | Any
|
Add a single-column uniqueness constraint for this column in Ferro.
Multi-column uniqueness is declared with |
_Unset
|
index
|
bool | Any
|
Request an index for this column in Ferro. |
_Unset
|
back_ref
|
bool | Any
|
Mark this field as a reverse relationship. This is the lower-level
equivalent of assigning |
_Unset
|
many_to_many
|
bool | Any
|
Mark this field as a many-to-many relationship. |
_Unset
|
related_name
|
str | None | Any
|
Reverse relationship field name used by many-to-many relationships. |
_Unset
|
through
|
str | None | Any
|
Optional join table name used by many-to-many relationships. |
_Unset
|
nullable
|
FerroNullable | Any
|
Alembic |
_Unset
|
default_factory
|
Callable[[], Any] | Callable[[dict[str, Any]], Any] | None
|
A callable to generate the default value. The callable can either take 0 arguments (in which case it is called as is) or a single argument containing the already validated data. |
_Unset
|
alias
|
str | None
|
The name to use for the attribute when validating or serializing by alias. This is often used for things like converting between snake and camel case. |
_Unset
|
alias_priority
|
int | None
|
Priority of the alias. This affects whether an alias generator is used. |
_Unset
|
validation_alias
|
str | AliasPath | AliasChoices | None
|
Like |
_Unset
|
serialization_alias
|
str | None
|
Like |
_Unset
|
title
|
str | None
|
Human-readable title. |
_Unset
|
field_title_generator
|
Callable[[str, FieldInfo], str] | None
|
A callable that takes a field name and returns title for it. |
_Unset
|
description
|
str | None
|
Human-readable description. |
_Unset
|
examples
|
list[Any] | None
|
Example values for this field. |
_Unset
|
exclude
|
bool | None
|
Whether to exclude the field from the model serialization. |
_Unset
|
exclude_if
|
Callable[[Any], bool] | None
|
A callable that determines whether to exclude a field during serialization based on its value. |
_Unset
|
discriminator
|
str | Discriminator | None
|
Field name or Discriminator for discriminating the type in a tagged union. |
_Unset
|
deprecated
|
Deprecated | str | bool | None
|
A deprecation message, an instance of |
_Unset
|
json_schema_extra
|
JsonDict | Callable[[JsonDict], None] | None
|
A dict or callable to provide extra JSON schema properties. |
_Unset
|
frozen
|
bool | None
|
Whether the field is frozen. If true, attempts to change the value on an instance will raise an error. |
_Unset
|
validate_default
|
bool | None
|
If |
_Unset
|
repr
|
bool
|
A boolean indicating whether to include the field in the |
_Unset
|
init
|
bool | None
|
Whether the field should be included in the constructor of the dataclass. (Only applies to dataclasses.) |
_Unset
|
init_var
|
bool | None
|
Whether the field should only be included in the constructor of the dataclass. (Only applies to dataclasses.) |
_Unset
|
kw_only
|
bool | None
|
Whether the field should be a keyword-only argument in the constructor of the dataclass. (Only applies to dataclasses.) |
_Unset
|
coerce_numbers_to_str
|
bool | None
|
Whether to enable coercion of any |
_Unset
|
strict
|
bool | None
|
If |
_Unset
|
gt
|
SupportsGt | None
|
Greater than. If set, value must be greater than this. Only applicable to numbers. |
_Unset
|
ge
|
SupportsGe | None
|
Greater than or equal. If set, value must be greater than or equal to this. Only applicable to numbers. |
_Unset
|
lt
|
SupportsLt | None
|
Less than. If set, value must be less than this. Only applicable to numbers. |
_Unset
|
le
|
SupportsLe | None
|
Less than or equal. If set, value must be less than or equal to this. Only applicable to numbers. |
_Unset
|
multiple_of
|
float | None
|
Value must be a multiple of this. Only applicable to numbers. |
_Unset
|
min_length
|
int | None
|
Minimum length for iterables. |
_Unset
|
max_length
|
int | None
|
Maximum length for iterables. |
_Unset
|
pattern
|
str | Pattern[str] | None
|
Pattern for strings (a regular expression). |
_Unset
|
allow_inf_nan
|
bool | None
|
Allow |
_Unset
|
max_digits
|
int | None
|
Maximum number of allow digits for strings. |
_Unset
|
decimal_places
|
int | None
|
Maximum number of decimal places allowed for numbers. |
_Unset
|
union_mode
|
Literal['smart', 'left_to_right']
|
The strategy to apply when validating a union. Can be |
_Unset
|
fail_fast
|
bool | None
|
If |
_Unset
|
extra
|
Unpack[_EmptyKwargs]
|
(Deprecated) Extra fields that will be included in the JSON schema. Warning The |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
A new [ |
Raises:
| Type | Description |
|---|---|
TypeError
|
If Ferro kwargs are provided together with callable |
Examples:
>>> from typing import Annotated
>>> from ferro import Field, Model
>>> class User(Model):
... id: int | None = Field(default=None, primary_key=True)
... username: str = Field(unique=True, min_length=3)
>>> class UserAnnotated(Model):
... id: Annotated[int | None, Field(default=None, primary_key=True)]
... username: Annotated[str, Field(unique=True, min_length=3)]
Source code in src/ferro/fields.py
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 | |
FerroField
¶
Store database column metadata for a model field
Attributes:
primary_key: Mark the field as the table primary key.
autoincrement: Override automatic increment behavior for primary key columns.
unique: Enforce a **single-column** uniqueness constraint for this column only.
For uniqueness on multiple columns together, declare
``__ferro_composite_uniques__`` on the :class:`ferro.models.Model` subclass
(see the models guide).
index: Request an index for the column.
nullable: Alembic ``Column.nullable`` when using :func:`~ferro.migrations.get_metadata`.
``'infer'`` (default) uses whether the annotation allows ``None``.
``False`` / ``True`` force NOT NULL / NULL regardless of the type (for
advanced cases such as ``int | None`` used only for static typing).
db_type: Optional canonical SQL-type override (:data:`DbType`). Use a
:data:`DbTypeToken` literal or :func:`varchar` for ``varchar(N)``.
Strict validation against the Python annotation runs at class-definition
time. Default ``None`` preserves today's behavior.
db_check: When ``True``, emit a DB-side ``CHECK`` constraint enforcing the
allowed values for closed-domain types (``enum.Enum`` subclasses).
Only valid in combination with ``db_type``; combining with default
native-enum storage is rejected as redundant. Default ``False``.
Examples:
>>> from typing import Annotated
>>> from ferro.models import Model
>>>
>>> class User(Model):
... id: Annotated[int, FerroField(primary_key=True)]
... email: Annotated[str, FerroField(unique=True, index=True)]
Source code in src/ferro/base.py
Attributes¶
primary_key = primary_key
instance-attribute
¶
autoincrement = autoincrement
instance-attribute
¶
unique = unique
instance-attribute
¶
index = index
instance-attribute
¶
nullable = _validate_nullable_option(nullable, 'FerroField')
instance-attribute
¶
db_type = db_type
instance-attribute
¶
db_check = db_check
instance-attribute
¶
Functions¶
__init__(primary_key=False, autoincrement=None, unique=False, index=False, nullable='infer', db_type=None, db_check=False)
¶
Initialize field metadata options
Args:
primary_key: Set to True when the field is the primary key.
autoincrement: Control whether the database auto-increments the value.
When not provided, the backend infers a default for integer primary keys.
unique: Set to True to enforce **single-column** uniqueness only.
index: Set to True to create a database index.
nullable: See :class:`FerroField` attribute ``nullable`` in the class docstring.
Examples:
>>> from typing import Annotated
>>> from ferro.models import Model
>>>
>>> class User(Model):
... id: Annotated[int, FerroField(primary_key=True)]
... created_at: Annotated[int, FerroField(index=True)]
Source code in src/ferro/base.py
FerroNullable = Literal['infer'] | bool
module-attribute
¶
Alembic column nullability: 'infer' from the field type, or forced bool.
varchar(length)
¶
Build the canonical varchar(N) token for :func:~ferro.Field db_type=.
Prefer this over a raw string so IDEs and type checkers see a deliberate Ferro vocabulary choice. Runtime validation still applies at model definition time.
Examples:
Source code in src/ferro/base.py
DbType = DbTypeToken | str
module-attribute
¶
Canonical db_type value: a :data:DbTypeToken or varchar(N) (see :func:varchar).
DbTypeToken = Literal['text', 'smallint', 'int', 'bigint', 'uuid', 'timestamp', 'timestamptz', 'date', 'time']
module-attribute
¶
Fixed canonical SQL storage tokens accepted by :func:~ferro.Field db_type=.