Field Metadata¶
Field(default=PydanticUndefined, *, primary_key=_Unset, autoincrement=_Unset, unique=_Unset, index=_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], *, 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 = ..., primary_key: bool = ..., autoincrement: bool | None = ..., unique: bool = ..., index: bool = ..., **extra: Any) -> Any
Field(default: 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 = ..., primary_key: bool = ..., autoincrement: bool | None = ..., unique: bool = ..., index: bool = ..., **extra: Any) -> Any
Field(default: _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] = ..., 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 = ..., primary_key: bool = ..., autoincrement: bool | None = ..., unique: bool = ..., index: bool = ..., **extra: Any) -> _T
Field(*, 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 = ..., primary_key: bool = ..., autoincrement: bool | None = ..., unique: bool = ..., index: bool = ..., **extra: Any) -> Any
Field(*, 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 = ..., primary_key: bool = ..., autoincrement: bool | None = ..., unique: bool = ..., index: bool = ..., **extra: Any) -> _T
Field(*, 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 = ..., primary_key: bool = ..., autoincrement: bool | None = ..., unique: bool = ..., index: bool = ..., **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 uniqueness constraint for this column in Ferro. |
_Unset
|
index
|
bool | Any
|
Request an index for this column in Ferro. |
_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 ferro import Field, Model
>>> class User(Model):
... id: int | None = Field(default=None, primary_key=True)
... username: str = Field(unique=True, min_length=3)
Source code in src/ferro/fields.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 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 | |
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 uniqueness constraint for the column.
index: Request an index for the column.
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
¶
Functions¶
__init__(primary_key=False, autoincrement=None, unique=False, index=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 uniqueness.
index: Set to True to create a database index.
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
ForeignKey
¶
Describe a forward foreign-key relationship between models
Attributes:
to: Target model class resolved during model binding.
related_name: Name of the reverse relationship attribute on the target model.
on_delete: Referential action applied when the parent row is deleted.
unique: Treat the relation as one-to-one when True.
Examples:
>>> from typing import Annotated
>>> from ferro.models import Model
>>>
>>> class User(Model):
... id: Annotated[int, FerroField(primary_key=True)]
>>>
>>> class Post(Model):
... id: Annotated[int, FerroField(primary_key=True)]
... author: Annotated[int, ForeignKey("posts", on_delete="CASCADE")]
Source code in src/ferro/base.py
Attributes¶
to = None
instance-attribute
¶
related_name = related_name
instance-attribute
¶
on_delete = on_delete
instance-attribute
¶
unique = unique
instance-attribute
¶
Functions¶
__init__(related_name, on_delete='CASCADE', unique=False)
¶
Initialize foreign-key relationship metadata
Args:
related_name: Name for reverse access from the related model.
on_delete: Referential action for parent deletion.
Common values include "CASCADE", "RESTRICT", "SET NULL", "SET DEFAULT", and "NO ACTION".
unique: Set to True to enforce one-to-one behavior.
Examples:
>>> from typing import Annotated
>>> from ferro.models import Model
>>>
>>> class User(Model):
... id: Annotated[int, FerroField(primary_key=True)]
... profile_id: Annotated[int, ForeignKey("user", unique=True)]
Source code in src/ferro/base.py
ManyToManyField
¶
Describe metadata for a many-to-many relationship
Attributes:
to: Target model class resolved during model binding.
related_name: Name of the reverse relationship attribute on the target model.
through: Optional join table name used for the association.
Examples:
>>> from typing import Annotated
>>> from ferro.models import Model
>>>
>>> class Tag(Model):
... id: Annotated[int, FerroField(primary_key=True)]
>>>
>>> class Post(Model):
... id: Annotated[int, FerroField(primary_key=True)]
... tags: Annotated[list[int], ManyToManyField("posts")]
Source code in src/ferro/base.py
Attributes¶
to = None
instance-attribute
¶
related_name = related_name
instance-attribute
¶
through = through
instance-attribute
¶
Functions¶
__init__(related_name, through=None)
¶
Initialize many-to-many relationship metadata
Args:
related_name: Name for reverse access from the related model.
through: Explicit join table name.
When omitted, Ferro generates a join table name automatically.
Examples:
>>> from typing import Annotated
>>> from ferro.models import Model
>>>
>>> class User(Model):
... id: Annotated[int, FerroField(primary_key=True)]
... teams: Annotated[list[int], ManyToManyField("members", through="team_members")]