# Field, FerroField and DbType

Fields are declared as type annotations on a [`Model`](<https://ferro-orm.x54.sh/api/model/index.md>), with `Field(...)` supplying database options (primary key, unique, index, defaults). The other entries on this page (`FerroField`, `FerroNullable`, `varchar`, `DbType` and `DbTypeToken`) describe the field metadata and the explicit column types `db_type=` takes. `fn` holds the database functions a generated column's expression or a check can call.

## Field

```python
Field(default: Any = PydanticUndefined, *, foreign_key: type[Any] | str | None | Any = _Unset, on_delete: OnDelete | Any = _Unset, primary_key: bool | Any = _Unset, autoincrement: bool | None | Any = _Unset, unique: bool | Any = _Unset, index: bool | Any = _Unset, nullable: FerroNullable | Any = _Unset, db_type: DbType | None | Any = _Unset, db_check: bool | Any = _Unset, renamed_from: str | None | Any = _Unset, retired: bool | Any = _Unset, generated: Callable[[Any], Any] | None | Any = _Unset, default_factory: Callable[[], Any] | Callable[[dict[str, Any]], Any] | None = _Unset, alias: str | None = _Unset, alias_priority: int | None = _Unset, validation_alias: str | AliasPath | AliasChoices | None = _Unset, serialization_alias: str | None = _Unset, title: str | None = _Unset, field_title_generator: Callable[[str, FieldInfo], str] | None = _Unset, description: str | None = _Unset, examples: list[Any] | None = _Unset, exclude: bool | None = _Unset, exclude_if: Callable[[Any], bool] | None = _Unset, discriminator: str | Discriminator | None = _Unset, deprecated: Deprecated | str | bool | None = _Unset, json_schema_extra: JsonDict | Callable[[JsonDict], None] | None = _Unset, frozen: bool | None = _Unset, validate_default: bool | None = _Unset, repr: bool = _Unset, init: bool | None = _Unset, init_var: bool | None = _Unset, kw_only: bool | None = _Unset, pattern: str | Pattern[str] | None = _Unset, strict: bool | None = _Unset, coerce_numbers_to_str: bool | None = _Unset, gt: SupportsGt | None = _Unset, ge: SupportsGe | None = _Unset, lt: SupportsLt | None = _Unset, le: SupportsLe | None = _Unset, multiple_of: float | None = _Unset, allow_inf_nan: bool | None = _Unset, max_digits: int | None = _Unset, decimal_places: int | None = _Unset, min_length: int | None = _Unset, max_length: int | None = _Unset, union_mode: Literal['smart', 'left_to_right'] = _Unset, fail_fast: bool | None = _Unset, **unknown: Any) -> Any
```

Declare a model field with Pydantic's options and Ferro's database options.

`Field` takes every argument of Pydantic's own `Field` and adds the database options Ferro reads: primary key, uniqueness, indexes, column type, foreign keys and rename hints. Use it as the field's default, or inside `Annotated[...]`. See [Field options](<https://ferro-orm.x54.sh/guide/models-and-fields/#field-options>) for each database option at work.

Parameters:

<table>       <thead>         <tr>           <th>Name</th>           <th>Type</th>           <th>Description</th>           <th>Default</th>         </tr>       </thead>       <tbody>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(default)">                  <code>default</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="typing.Any" optional="">Any</autoref></code>             </td>             <td>               <div class="doc-md-description">                 <p>The value the field takes when it is not set.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic_core.PydanticUndefined" optional="">PydanticUndefined</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(foreign_key)">                  <code>foreign_key</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="type" optional="">type</autoref>[<autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="typing.Any" optional="">Any</autoref>] | <autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="str" optional="">str</autoref> | None | <autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="typing.Any" optional="">Any</autoref></code>             </td>             <td>               <div class="doc-md-description">                 <p>Make the column a foreign key to this model's primary key: the class, or its class name for a self reference or a model defined later. The column's type must be the target key's type, and a type that stores differently needs the target's <code>db_type</code>. See <a href="https://ferro-orm.x54.sh/guide/relationships/index.md">Relationships</a>.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(on_delete)">                  <code>on_delete</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="ferro.base.OnDelete" optional="">OnDelete</autoref> | <autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="typing.Any" optional="">Any</autoref></code>             </td>             <td>               <div class="doc-md-description">                 <p>What the database does to this row when the row the key points at is deleted: <code>&quot;CASCADE&quot;</code> (the default), <code>&quot;RESTRICT&quot;</code>, <code>&quot;SET NULL&quot;</code>, <code>&quot;SET DEFAULT&quot;</code> or <code>&quot;NO ACTION&quot;</code>. Needs <code>foreign_key</code>, and <code>&quot;SET NULL&quot;</code> needs an annotation that allows <code>None</code>.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(primary_key)">                  <code>primary_key</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="bool" optional="">bool</autoref> | <autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="typing.Any" optional="">Any</autoref></code>             </td>             <td>               <div class="doc-md-description">                 <p>Make the column the table's primary key.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(autoincrement)">                  <code>autoincrement</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="bool" optional="">bool</autoref> | None | <autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="typing.Any" optional="">Any</autoref></code>             </td>             <td>               <div class="doc-md-description">                 <p>Whether the database generates the key. When not given, an integer primary key auto-increments.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(unique)">                  <code>unique</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="bool" optional="">bool</autoref> | <autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="typing.Any" optional="">Any</autoref></code>             </td>             <td>               <div class="doc-md-description">                 <p>Add a unique constraint on this column alone. For uniqueness across several columns, declare <code>__ferro_composite_uniques__</code> on the model (see <a href="https://ferro-orm.x54.sh/guide/models-and-fields/#composite-uniques">Composite uniques</a>).</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(index)">                  <code>index</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="bool" optional="">bool</autoref> | <autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="typing.Any" optional="">Any</autoref></code>             </td>             <td>               <div class="doc-md-description">                 <p>Add an index on the column.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(nullable)">                  <code>nullable</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="ferro.base.FerroNullable" optional="">FerroNullable</autoref> | <autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="typing.Any" optional="">Any</autoref></code>             </td>             <td>               <div class="doc-md-description">                 <p><code>&quot;infer&quot;</code> (the default) makes the column nullable when the annotation allows <code>None</code>; <code>True</code> or <code>False</code> forces <code>NULL</code> or <code>NOT NULL</code> in every schema Ferro emits, whatever the annotation says.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(db_type)">                  <code>db_type</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="ferro.base.DbType" optional="">DbType</autoref> | None | <autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="typing.Any" optional="">Any</autoref></code>             </td>             <td>               <div class="doc-md-description">                 <p>The column's SQL type, overriding the one Ferro derives from the annotation: a <autoref data-zensical-autoref="" domain="py" filepath="/home/runner/work/ferro-orm/ferro-orm/src/ferro/fields.py" identifier="ferro.DbTypeToken" lineno="622" origin="ferro.Field" role="func"><code>DbTypeToken</code></autoref> or a <autoref data-zensical-autoref="" domain="py" filepath="/home/runner/work/ferro-orm/ferro-orm/src/ferro/fields.py" identifier="ferro.varchar" lineno="622" origin="ferro.Field" role="func"><code>varchar(N)</code></autoref> name. When not given, the column takes the derived type. A type the annotation cannot be stored as is refused when the class is defined.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(db_check)">                  <code>db_check</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="bool" optional="">bool</autoref> | <autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="typing.Any" optional="">Any</autoref></code>             </td>             <td>               <div class="doc-md-description">                 <p>Add a <code>CHECK</code> constraint, named <code>ck_&lt;table&gt;_&lt;column&gt;</code>, that limits the column to an enum's values. Needs <code>db_type</code>, since the default native enum type already enforces them. Defaults to <code>False</code>.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(renamed_from)">                  <code>renamed_from</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="str" optional="">str</autoref> | None | <autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="typing.Any" optional="">Any</autoref></code>             </td>             <td>               <div class="doc-md-description">                 <p>The column's previous name, so that <code>ferro migrate new</code> renames the column, and every index and check named after it, instead of dropping and adding it. See <a href="https://ferro-orm.x54.sh/guide/schema/renames-and-retirements/#rename-hints">Rename hints</a>.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(retired)">                  <code>retired</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="bool" optional="">bool</autoref> | <autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="typing.Any" optional="">Any</autoref></code>             </td>             <td>               <div class="doc-md-description">                 <p>Take the column out of the model while it stays in the schema, nullable, until a later release deletes the declaration. The annotation must allow <code>None</code>. Defaults to <code>False</code>. See <a href="https://ferro-orm.x54.sh/guide/schema/renames-and-retirements/#retiring-a-column">Retiring a column</a>.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(generated)">                  <code>generated</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="collections.abc.Callable" optional="">Callable</autoref>[[<autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="typing.Any" optional="">Any</autoref>], <autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="typing.Any" optional="">Any</autoref>] | None | <autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="typing.Any" optional="">Any</autoref></code>             </td>             <td>               <div class="doc-md-description">                 <p>A lambda over the model's own columns, its parameter named after the model, from which the database computes the column on every write: arithmetic and the functions in <autoref data-zensical-autoref="" domain="py" filepath="/home/runner/work/ferro-orm/ferro-orm/src/ferro/fields.py" identifier="ferro.fn" lineno="622" origin="ferro.Field" role="func"><code>ferro.fn</code></autoref>. The field defaults to <code>None</code> and holds the database's value once saved. Defaults to <code>None</code>, a plain column.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(default_factory)">                  <code>default_factory</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="collections.abc.Callable" optional="">Callable</autoref>[[], <autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="typing.Any" optional="">Any</autoref>] | <autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="collections.abc.Callable" optional="">Callable</autoref>[[<autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="dict" optional="">dict</autoref>[<autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="str" optional="">str</autoref>, <autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="typing.Any" optional="">Any</autoref>]], <autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="typing.Any" optional="">Any</autoref>] | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>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.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(alias)">                  <code>alias</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="str" optional="">str</autoref> | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>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.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(alias_priority)">                  <code>alias_priority</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="int" optional="">int</autoref> | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>Priority of the alias. This affects whether an alias generator is used.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(validation_alias)">                  <code>validation_alias</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="str" optional="">str</autoref> | <autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.aliases.AliasPath" optional="">AliasPath</autoref> | <autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.aliases.AliasChoices" optional="">AliasChoices</autoref> | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>Like <code>alias</code>, but only affects validation, not serialization.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(serialization_alias)">                  <code>serialization_alias</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="str" optional="">str</autoref> | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>Like <code>alias</code>, but only affects serialization, not validation.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(title)">                  <code>title</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="str" optional="">str</autoref> | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>Human-readable title.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(field_title_generator)">                  <code>field_title_generator</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="collections.abc.Callable" optional="">Callable</autoref>[[<autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="str" optional="">str</autoref>, <autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields.FieldInfo" optional="">FieldInfo</autoref>], <autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="str" optional="">str</autoref>] | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>A callable that takes a field name and returns title for it.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(description)">                  <code>description</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="str" optional="">str</autoref> | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>Human-readable description.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(examples)">                  <code>examples</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="list" optional="">list</autoref>[<autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="typing.Any" optional="">Any</autoref>] | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>Example values for this field.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(exclude)">                  <code>exclude</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="bool" optional="">bool</autoref> | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>Whether to exclude the field from the model serialization.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(exclude_if)">                  <code>exclude_if</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="collections.abc.Callable" optional="">Callable</autoref>[[<autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="typing.Any" optional="">Any</autoref>], <autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="bool" optional="">bool</autoref>] | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>A callable that determines whether to exclude a field during serialization based on its value.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(discriminator)">                  <code>discriminator</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="str" optional="">str</autoref> | <autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.Discriminator" optional="">Discriminator</autoref> | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>Field name or Discriminator for discriminating the type in a tagged union.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(deprecated)">                  <code>deprecated</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields.Deprecated" optional="">Deprecated</autoref> | <autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="str" optional="">str</autoref> | <autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="bool" optional="">bool</autoref> | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>A deprecation message, an instance of <code>warnings.deprecated</code> or the <code>typing_extensions.deprecated</code> backport, or a boolean. If <code>True</code>, a default deprecation message will be emitted when accessing the field.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(json_schema_extra)">                  <code>json_schema_extra</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.config.JsonDict" optional="">JsonDict</autoref> | <autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="collections.abc.Callable" optional="">Callable</autoref>[[<autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.config.JsonDict" optional="">JsonDict</autoref>], None] | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>A dict or callable to provide extra JSON schema properties.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(frozen)">                  <code>frozen</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="bool" optional="">bool</autoref> | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>Whether the field is frozen. If true, attempts to change the value on an instance will raise an error.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(validate_default)">                  <code>validate_default</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="bool" optional="">bool</autoref> | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>If <code>True</code>, apply validation to the default value every time you create an instance. Otherwise, for performance reasons, the default value of the field is trusted and not validated.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(repr)">                  <code>repr</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="bool" optional="">bool</autoref></code>             </td>             <td>               <div class="doc-md-description">                 <p>A boolean indicating whether to include the field in the <code>__repr__</code> output.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(init)">                  <code>init</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="bool" optional="">bool</autoref> | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>Whether the field should be included in the constructor of the dataclass. (Only applies to dataclasses.)</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(init_var)">                  <code>init_var</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="bool" optional="">bool</autoref> | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>Whether the field should <em>only</em> be included in the constructor of the dataclass. (Only applies to dataclasses.)</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(kw_only)">                  <code>kw_only</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="bool" optional="">bool</autoref> | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>Whether the field should be a keyword-only argument in the constructor of the dataclass. (Only applies to dataclasses.)</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(coerce_numbers_to_str)">                  <code>coerce_numbers_to_str</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="bool" optional="">bool</autoref> | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>Whether to enable coercion of any <code>Number</code> type to <code>str</code> (not applicable in <code>strict</code> mode).</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(strict)">                  <code>strict</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="bool" optional="">bool</autoref> | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>If <code>True</code>, strict validation is applied to the field. See <a href="https://docs.pydantic.dev/latest/concepts/strict_mode/">Strict Mode</a>.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(gt)">                  <code>gt</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="annotated_types.SupportsGt" optional="">SupportsGt</autoref> | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>Greater than. If set, value must be greater than this. Only applicable to numbers.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(ge)">                  <code>ge</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="annotated_types.SupportsGe" optional="">SupportsGe</autoref> | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>Greater than or equal. If set, value must be greater than or equal to this. Only applicable to numbers.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(lt)">                  <code>lt</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="annotated_types.SupportsLt" optional="">SupportsLt</autoref> | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>Less than. If set, value must be less than this. Only applicable to numbers.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(le)">                  <code>le</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="annotated_types.SupportsLe" optional="">SupportsLe</autoref> | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>Less than or equal. If set, value must be less than or equal to this. Only applicable to numbers.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(multiple_of)">                  <code>multiple_of</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="float" optional="">float</autoref> | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>Value must be a multiple of this. Only applicable to numbers.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(min_length)">                  <code>min_length</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="int" optional="">int</autoref> | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>Minimum length for iterables.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(max_length)">                  <code>max_length</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="int" optional="">int</autoref> | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>Maximum length for iterables.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(pattern)">                  <code>pattern</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="str" optional="">str</autoref> | <autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="re.Pattern" optional="">Pattern</autoref>[<autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="str" optional="">str</autoref>] | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>Pattern for strings (a regular expression).</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(allow_inf_nan)">                  <code>allow_inf_nan</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="bool" optional="">bool</autoref> | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>Allow <code>inf</code>, <code>-inf</code>, <code>nan</code>. Only applicable to float and <autoref data-zensical-autoref="" domain="py" filepath="/home/runner/work/ferro-orm/ferro-orm/src/ferro/fields.py" identifier="decimal.Decimal" lineno="622" origin="ferro.Field" role="func"><code>Decimal</code></autoref> numbers.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(max_digits)">                  <code>max_digits</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="int" optional="">int</autoref> | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>Maximum number of allowed digits for strings.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(decimal_places)">                  <code>decimal_places</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="int" optional="">int</autoref> | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>Maximum number of decimal places allowed for numbers.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(union_mode)">                  <code>union_mode</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="typing.Literal" optional="">Literal</autoref>['smart', 'left_to_right']</code>             </td>             <td>               <div class="doc-md-description">                 <p>The strategy to apply when validating a union. Can be <code>smart</code> (the default), or <code>left_to_right</code>. See <a href="https://docs.pydantic.dev/latest/concepts/unions/#union-modes">Union Mode</a>.</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(fail_fast)">                  <code>fail_fast</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" identifier="bool" optional="">bool</autoref> | None</code>             </td>             <td>               <div class="doc-md-description">                 <p>If <code>True</code>, validation will stop on the first error. If <code>False</code>, all validation errors will be collected. This option can be applied only to iterable types (list, tuple, set, and frozenset).</p>                </div>             </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="pydantic.fields._Unset" optional="">_Unset</autoref></code>             </td>           </tr>           <tr class="doc-section-item">             <td> <h3 class="doc doc-heading doc-heading-parameter" id="ferro.Field(**unknown)">                  <code>**unknown</code> </h3>            </td>             <td>                   <code><autoref backlink-anchor="ferro.Field" backlink-type="used-by" hover="" identifier="typing.Any" optional="">Any</autoref></code>             </td>             <td>               <div class="doc-md-description">                 <p>Refused: a keyword not listed here raises <code>TypeError</code> naming the closest one that is. A relation keyword <code>Field</code> no longer takes (<code>back_ref</code>, <code>many_to_many</code>, <code>related_name</code>, <code>through</code>, <code>reverse_index</code>) names what replaces it; see <a href="https://ferro-orm.x54.sh/upgrading/#explicit-foreign-keys">Upgrading</a>.</p>                </div>             </td>             <td>                   <code>{}</code>             </td>           </tr>       </tbody>     </table>

Returns:

| Type | Description |
| --- | --- |
| `Any` | A new `FieldInfo` that carries the database options in its `metadata`, as one `FerroField`, and never in its JSON schema. The return annotation is `Any` so that `Field` type-checks as any field's default. |

Raises:

| Type | Description |
| --- | --- |
| `TypeError` | A keyword `Field` does not take, or `nullable`, `renamed_from`, `retired`, `generated`, `foreign_key` or `on_delete` has a value of the wrong type. |

Examples:

```python
from ferro import Field, Model, create_tables


class Reader(Model):
    id: int | None = Field(default=None, primary_key=True)
    handle: str = Field(unique=True, min_length=3)
    nickname: str | None = Field(default=None, index=True)


await create_tables()
reader = await Reader.create(handle="ada")
assert reader.id == 1
```

The same model with the options inside `Annotated`:

```python
from typing import Annotated

from ferro import Field, Model


class Reader(Model):
    id: Annotated[int | None, Field(primary_key=True)] = None
    handle: Annotated[str, Field(unique=True, min_length=3)]
    nickname: Annotated[str | None, Field(index=True)] = None
```

## FerroField

```python
FerroField(primary_key: bool = False, autoincrement: bool | None = None, unique: bool = False, index: bool = False, nullable: FerroNullable = 'infer', db_type: DbType | None = None, db_check: bool = False, renamed_from: str | None = None, retired: bool = False, generated: Any = None, foreign_key: type | str | None = None, on_delete: OnDelete | None = None)
```

Database options for one model field, given inside `Annotated[...]`.

`Field` takes the same options and is the usual spelling: it stores them as one `FerroField` in the field's metadata, never in its JSON schema. `FerroField` carries only the database options, for an `Annotated` declaration that keeps them apart from Pydantic's own `Field`. See [Field options](<https://ferro-orm.x54.sh/guide/models-and-fields/#field-options>) for each option at work.

Examples:

```python
from typing import Annotated

from ferro import FerroField, Model, create_tables


class Tag(Model):
    id: Annotated[int | None, FerroField(primary_key=True)] = None
    label: Annotated[str, FerroField(unique=True, index=True)]


await create_tables()
tag = await Tag.create(label="history")
assert tag.id == 1
```

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `primary_key` | `bool` | Make the column the table's primary key. | `False` |
| `autoincrement` | `bool \| None` | Whether the database generates the key. Defaults to `None`, which lets an integer primary key auto-increment. | `None` |
| `unique` | `bool` | Add a unique constraint on this column alone. For uniqueness across several columns, declare `__ferro_composite_uniques__` on the model (see [Composite uniques](<https://ferro-orm.x54.sh/guide/models-and-fields/#composite-uniques>)). | `False` |
| `index` | `bool` | Add an index on the column. | `False` |
| `nullable` | `FerroNullable` | `"infer"` (the default) makes the column nullable when the annotation allows `None`; `True` or `False` forces `NULL` or `NOT NULL` whatever the annotation says. | `'infer'` |
| `db_type` | `DbType \| None` | The column's SQL type, overriding the one Ferro derives from the annotation: a `DbTypeToken` or a `varchar(N)` name. Defaults to `None`, the derived type. A type the annotation cannot be stored as is refused when the class is defined. | `None` |
| `db_check` | `bool` | Add a `CHECK` constraint that limits the column to an enum's values. Needs `db_type`, since the default native enum type already enforces them. Defaults to `False`. | `False` |
| `renamed_from` | `str \| None` | The column's previous name, so that `ferro migrate new` renames the column instead of dropping and adding it. See [Rename hints](<https://ferro-orm.x54.sh/guide/schema/renames-and-retirements/#rename-hints>). | `None` |
| `retired` | `bool` | Take the column out of the model while it stays in the schema, nullable, until a later release deletes the declaration. The annotation must allow `None`. Defaults to `False`. See [Retiring a column](<https://ferro-orm.x54.sh/guide/schema/renames-and-retirements/#retiring-a-column>). | `False` |
| `generated` | `Any` | A lambda over the model's own columns from which the database computes the column on every write. Defaults to `None`, a plain column. Give the field a `None` default beside it, as `Field` does for you. | `None` |
| `foreign_key` | `type \| str \| None` | The model whose primary key the column holds: the class, or its class name for a self reference or a model defined later. Defaults to `None`, a plain column. | `None` |
| `on_delete` | `OnDelete \| None` | What the database does to this row when the row the key points at is deleted: `"CASCADE"`, `"RESTRICT"`, `"SET NULL"`, `"SET DEFAULT"` or `"NO ACTION"`. Defaults to `None`, which is `"CASCADE"` on a foreign-key column. | `None` |

Raises:

| Type | Description |
| --- | --- |
| `TypeError` | `nullable`, `renamed_from`, `retired`, `generated`, `foreign_key` or `on_delete` has a value of the wrong type. |

Attributes:

| Name | Type | Description |
| --- | --- | --- |
| `primary_key` |  | Whether the column is the table's primary key. |
| `autoincrement` |  | Whether the database generates the key; `None` lets Ferro decide. |
| `unique` |  | Whether the column has a single-column unique constraint. |
| `index` |  | Whether the column has an index. |
| `nullable` |  | `"infer"`, or a forced `True` or `False`. |
| `db_type` |  | The SQL type override, or `None` for the derived type. |
| `db_check` |  | Whether the column has a `CHECK` constraint on an enum's values. |
| `renamed_from` |  | The column's previous name, or `None`. |
| `retired` |  | Whether the column is retired from the model. |
| `generated` |  | The lambda the database computes the column from, or `None`. |
| `foreign_key` |  | The model the column's key points at, its class name, or `None`. |
| `on_delete` | `str \| None` | The referential action on a foreign-key column, or `None` when unset. |

### primary\_key

```python
primary_key = primary_key
```

Whether the column is the table's primary key.

### autoincrement

```python
autoincrement = autoincrement
```

Whether the database generates the key; `None` lets Ferro decide.

### unique

```python
unique = unique
```

Whether the column has a single-column unique constraint.

### index

```python
index = index
```

Whether the column has an index.

### nullable

```python
nullable = _validate_nullable_option(nullable, 'FerroField')
```

`"infer"`, or a forced `True` or `False`.

### db\_type

```python
db_type = db_type
```

The SQL type override, or `None` for the derived type.

### db\_check

```python
db_check = db_check
```

Whether the column has a `CHECK` constraint on an enum's values.

### renamed\_from

```python
renamed_from = _validate_renamed_from(renamed_from, 'FerroField')
```

The column's previous name, or `None`.

### retired

```python
retired = _validate_retired(retired, 'FerroField')
```

Whether the column is retired from the model.

### generated

```python
generated = _validate_generated(generated, 'FerroField')
```

The lambda the database computes the column from, or `None`.

### foreign\_key

```python
foreign_key = _validate_foreign_key(foreign_key, 'FerroField')
```

The model the column's key points at, its class name, or `None`.

### on\_delete

```python
on_delete: str | None = 'CASCADE' if on_delete is None and foreign_key is not None else on_delete
```

The referential action on a foreign-key column, or `None` when unset.

A foreign-key column declared without one holds `"CASCADE"`.

## FerroNullable

```python
FerroNullable = Literal['infer'] | bool
```

Column nullability: `"infer"` follows the annotation, `True` or `False` forces it.

With `"infer"`, a column is nullable when its annotation allows `None`. `True` and `False` force `NULL` and `NOT NULL` in every schema Ferro emits, whatever the annotation says.

## varchar

```python
varchar(length: int) -> str
```

Build the `varchar(N)` type name for `db_type=`.

A call to `varchar` reads as a deliberate type choice, where a raw `"varchar(80)"` string could be a typo. Ferro still checks the value against the field's annotation when the model class is defined.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `length` | `int` | The maximum length in characters. Must be positive. | *required* |

Returns:

| Type | Description |
| --- | --- |
| `str` | The type name, `"varchar(<length>)"`. |

Raises:

| Type | Description |
| --- | --- |
| `ValueError` | `length` is zero or negative. |

Examples:

```python
from ferro import varchar

assert varchar(80) == "varchar(80)"
```

## DbType

```python
DbType = DbTypeToken | str
```

A `db_type=` value: a `DbTypeToken` or a `varchar(N)` string.

Build the `varchar(N)` form with `varchar`.

## DbTypeToken

```python
DbTypeToken = Literal['text', 'smallint', 'int', 'bigint', 'uuid', 'timestamp', 'timestamptz', 'date', 'time', 'json', 'jsonb']
```

SQL storage type names accepted by `db_type=` on `Field`.

See [Column type overrides with `db_type`](<https://ferro-orm.x54.sh/guide/models-and-fields/#column-type-overrides-with-db_type>) for the types each name maps to on SQLite and Postgres.

## fn

SQL functions for a generated column's expression.

Each function here is one the database evaluates, with the same result on SQLite and Postgres. Call it on a column inside a `Field(generated=...)` lambda, or on either side of a `Check` comparison; it builds an expression the database computes, never a Python value. An expression combines these functions with columns, literals, `+ - * /` and unary `-`. [Generated columns](<https://ferro-orm.x54.sh/guide/models-and-fields/#generated-columns>) lists the rules an expression must follow to store the same value on both databases.

Examples:

```python
from ferro import Field, Model, create_tables, fn


class Transaction(Model):
    id: int | None = Field(default=None, primary_key=True)
    amount_minor: int
    direction: int = Field(
        generated=lambda transaction: fn.sign(transaction.amount_minor)
    )


await create_tables()
transaction = await Transaction.create(amount_minor=-250)
assert transaction.direction == -1
```

Functions:

| Name | Description |
| --- | --- |
| `sign` | Return -1, 0 or 1 by the sign of a number. |
| `abs` | Return a number without its sign. |
| `least` | Return the smallest of two or more numbers or dates. |
| `greatest` | Return the largest of two or more numbers or dates. |
| `coalesce` | Return the first argument that is not `NULL`. |
| `length` | Return the number of characters in a text. |
| `year` | Return the year of a date. |
| `month` | Return the month of a date, from 1 to 12. |
| `day` | Return the day of the month of a date, from 1 to 31. |

### sign

```python
sign(value: Any) -> Any
```

Return -1, 0 or 1 by the sign of a number.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `value` | `Any` | An integer, float or decimal column or expression. | *required* |

Returns:

| Type | Description |
| --- | --- |
| `Any` | The expression the database computes, of the argument's type: `-1` for a negative value, `0` for zero and `1` for a positive one. |

### abs

```python
abs(value: Any) -> Any
```

Return a number without its sign.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `value` | `Any` | An integer, float or decimal column or expression. | *required* |

Returns:

| Type | Description |
| --- | --- |
| `Any` | The expression the database computes, of the argument's type. |

### least

```python
least(first: Any, second: Any, *more: Any) -> Any
```

Return the smallest of two or more numbers or dates.

Every argument must be non-nullable, since the two databases disagree on a `NULL`; wrap a nullable column in `coalesce`. Text is not accepted: Postgres orders it by collation and SQLite byte by byte.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `first` | `Any` | An integer, float, decimal or date column or expression. | *required* |
| `second` | `Any` | A value of the same type. | *required* |
| `*more` | `Any` | More values of the same type. | `()` |

Returns:

| Type | Description |
| --- | --- |
| `Any` | The expression the database computes, of the arguments' type. |

### greatest

```python
greatest(first: Any, second: Any, *more: Any) -> Any
```

Return the largest of two or more numbers or dates.

Every argument must be non-nullable, as for `least`.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `first` | `Any` | An integer, float, decimal or date column or expression. | *required* |
| `second` | `Any` | A value of the same type. | *required* |
| `*more` | `Any` | More values of the same type. | `()` |

Returns:

| Type | Description |
| --- | --- |
| `Any` | The expression the database computes, of the arguments' type. |

### coalesce

```python
coalesce(first: Any, second: Any, *more: Any) -> Any
```

Return the first argument that is not `NULL`.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `first` | `Any` | An integer, float, decimal, text, date or datetime column or expression. | *required* |
| `second` | `Any` | A value of the same type, such as a literal fallback. | *required* |
| `*more` | `Any` | More values of the same type. | `()` |

Returns:

| Type | Description |
| --- | --- |
| `Any` | The expression the database computes, of the arguments' type. It is `NULL` only when every argument is. |

### length

```python
length(value: Any) -> Any
```

Return the number of characters in a text.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `value` | `Any` | A text column or expression. | *required* |

Returns:

| Type | Description |
| --- | --- |
| `Any` | The expression the database computes, an integer. |

### year

```python
year(value: Any) -> Any
```

Return the year of a date.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `value` | `Any` | A date column. A datetime is refused: its year depends on the session's time zone. | *required* |

Returns:

| Type | Description |
| --- | --- |
| `Any` | The expression the database computes, an integer. |

### month

```python
month(value: Any) -> Any
```

Return the month of a date, from 1 to 12.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `value` | `Any` | A date column. A datetime is refused: its month depends on the session's time zone. | *required* |

Returns:

| Type | Description |
| --- | --- |
| `Any` | The expression the database computes, an integer. |

### day

```python
day(value: Any) -> Any
```

Return the day of the month of a date, from 1 to 31.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `value` | `Any` | A date column. A datetime is refused: its day depends on the session's time zone. | *required* |

Returns:

| Type | Description |
| --- | --- |
| `Any` | The expression the database computes, an integer. |
