Ecto.Schema.field

You're seeing just the macro field, go back to Ecto.Schema module for more information.
Link to this macro

field(name, type \\ :string, opts \\ [])

View Source (macro)

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 like DateTime.utc_now or Ecto.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.

    Once a default value is set, if you send changes to the changeset that contains the same value defined as default, validations will not be performed since there are no changes after all.

  • :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 of true 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 as from p in Post, select: p. Defaults to true.

  • :redact - When true, it will display a value of **redacted** when inspected in changes inside a Ecto.Changeset and be excluded from inspect on the schema. Defaults to false.