Ecto.Migration.add
add
, go back to Ecto.Migration module for more information.
Adds a column when creating or altering a table.
This function also accepts Ecto primitive types as column types
that are normalized by the database adapter. For example,
:string
is converted to :varchar
, :binary
to :bits
or :blob
,
and so on.
However, the column type is not always the same as the type used in your
schema. For example, a schema that has a :string
field can be supported by
columns of type :char
, :varchar
, :text
, and others. For this reason,
this function also accepts :text
and other type annotations that are native
to the database. These are passed to the database as-is.
To sum up, the column type may be either an Ecto primitive type,
which is normalized in cases where the database does not understand it,
such as :string
or :binary
, or a database type which is passed as-is.
Custom Ecto types like Ecto.UUID
are not supported because
they are application-level concerns and may not always map to the database.
Examples
create table("posts") do
add :title, :string, default: "Untitled"
end
alter table("posts") do
add :summary, :text # Database type
add :object, :map # Elixir type which is handled by the database
end
Options
:primary_key
- whentrue
, marks this field as the primary key. If multiple fields are marked, a composite primary key will be created.:default
- the column's default value. It can be a string, number, empty list, list of strings, list of numbers, or a fragment generated byfragment/1
.:null
- whenfalse
, the column does not allow null values.:size
- the size of the type (for example, the number of characters). The default is no size, except for:string
, which defaults to255
.:precision
- the precision for a numeric type. Required when:scale
is specified.:scale
- the scale of a numeric type. Defaults to0
.:after
- positions field after the specified one. Only supported on MySQL, it is ignored by other databases.