Ecto.Schema.field
field
, go back to Ecto.Schema module for more information.
Defines a field on the schema with given name and type.
The field name will be used as is to read and write to the database
by all of the built-in adapters unless overridden with the :source
option.
Options
:default
- Sets the default value on the schema and the struct. The default value is calculated at compilation time, so don't use expressions likeDateTime.utc_now
orEcto.UUID.generate
as they would then be the same for all records: in this scenario you can use the:autogenerate
option to generate at insertion time.:source
- Defines the name that is to be used in database for this field. This is useful when attaching to an existing database. The value should be an atom.:autogenerate
- a{module, function, args}
tuple for a function to call to generate the field value before insertion if value is not set. A shorthand value oftrue
is equivalent to{type, :autogenerate, []}
.:read_after_writes
- When true, the field is always read back from the database after insert and updates.For relational databases, this means the RETURNING option of those statements is used. For this reason, MySQL does not support this option and will raise an error if a schema is inserted/updated with read after writes fields.
:virtual
- When true, the field is not persisted to the database. Notice virtual fields do not support:autogenerate
nor:read_after_writes
.:primary_key
- When true, the field is used as part of the composite primary key.:load_in_query
- When false, the field will not be loaded when selecting the whole struct in a query, such asfrom p in Post, select: p
. Defaults totrue
.:redact
- When true, it will display a value of**redacted**
when inspected in changes inside aEcto.Changeset
and be excluded from inspect on the schema. Defaults tofalse
.