Ecto v3.0.1 Ecto.Schema View Source
Defines a schema.
An Ecto schema is used to map any data source into an Elixir struct.
The definition of the schema is possible through two main APIs:
schema/2
and embedded_schema/1
.
schema/2
is typically used to map data from a persisted source,
usually a database table, into Elixir structs and vice-versa. For
this reason, the first argument of schema/2
is the source (table)
name. Structs defined with schema/2
also contain a __meta__
field
with metadata holding the status of the struct, for example, if it
has been built, loaded or deleted.
On the other hand, embedded_schema/1
is used for defining schemas
that are embedded in other schemas or only exist in-memory. For example,
you can use such schemas to receive data from a command line interface
and validate it, without ever persisting it elsewhere. Such structs
do not contain a __meta__
field, as they are never persisted.
Besides working as data mappers, embedded_schema/1
and schema/2
can
also be used together to decouple how the data is represented in your
applications from the database. Let’s see some examples.
Example
defmodule User do
use Ecto.Schema
schema "users" do
field :name, :string
field :age, :integer, default: 0
has_many :posts, Post
end
end
By default, a schema will automatically generate a primary key which is named
id
and of type :integer
. The field
macro defines a field in the schema
with given name and type. has_many
associates many posts with the user
schema. Schemas are regular structs and can be created and manipulated directly
using Elixir’s struct API:
iex> user = %User{name: "jane"}
iex> %{user | age: 30}
However, most commonly, structs are cast, validated and manipulated with the
Ecto.Changeset
module.
Note that the name of the database table does not need to correlate to your module name. For example, if you are working with a legacy database, you can reference the table name when you define your schema:
defmodule User do
use Ecto.Schema
schema "legacy_users" do
# ... fields ...
end
end
Embedded schemas are defined similarly to source-based schemas. For example, you can use an embedded schema to represent your UI, mapping and validating its inputs, and then you convert such embedded schema to other schemas that are persisted to the database:
defmodule SignUp do
use Ecto.Schema
embedded_schema do
field :name, :string
field :age, :integer
field :email, :string
field :accepts_conditions, :boolean
end
end
defmodule Profile do
use Ecto.Schema
schema "profiles" do
field :name
field :age
belongs_to :account, Account
end
end
defmodule Account do
use Ecto.Schema
schema "accounts" do
field :email
end
end
The SignUp
schema can be cast and validated with the help of the
Ecto.Changeset
module, and afterwards, you can copy its data to
the Profile
and Account
structs that will be persisted to the
database with the help of Ecto.Repo
.
Schema attributes
Supported attributes, to be set beforehand, for configuring the defined schema.
These attributes are:
@primary_key
- configures the schema primary key. It expects a tuple{field_name, type, options}
with the primary key field name, type (typically:id
or:binary_id
, but can be any type) and options. Defaults to{:id, :id, autogenerate: true}
. When set tofalse
, does not define a primary key in the schema unless composite keys are defined using the options offield
.@schema_prefix
- configures the schema prefix. Defaults tonil
, which generates structs and queries without prefix. When set, the prefix will be used by every built struct and on queries whenever the schema is used in afrom
or ajoin
. In PostgreSQL, the prefix is called “SCHEMA” (typically set via Postgres’search_path
). In MySQL the prefix points to databases.@foreign_key_type
- configures the default foreign key type used bybelongs_to
associations. Defaults to:id
;@timestamps_opts
- configures the default timestamps type used bytimestamps
. Defaults to[type: :naive_datetime]
;@derive
- the same as@derive
available inKernel.defstruct/1
as the schema defines a struct behind the scenes;@field_source_mapper
- a function that receives the current field name and returns the mapping of this field name in the underlying source. In other words, it is a mechanism to automatically generate the:source
option for thefield
macro. It defaults tofn x -> x end
, where no field transformation is done;
The advantage of configuring the schema via those attributes is that they can be set with a macro to configure application wide defaults.
For example, if your database does not support autoincrementing
primary keys and requires something like UUID or a RecordID, you
can configure and use :binary_id
as your primary key type as follows:
# Define a module to be used as base
defmodule MyApp.Schema do
defmacro __using__(_) do
quote do
use Ecto.Schema
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
end
end
end
# Now use MyApp.Schema to define new schemas
defmodule MyApp.Comment do
use MyApp.Schema
schema "comments" do
belongs_to :post, MyApp.Post
end
end
Any schemas using MyApp.Schema
will get the :id
field with type
:binary_id
as the primary key. We explain what the :binary_id
type
entails in the next section.
The belongs_to
association on MyApp.Comment
will also define
a :post_id
field with :binary_id
type that references the :id
field of the MyApp.Post
schema.
Primary keys
Ecto supports two ID types, called :id
and :binary_id
, which are
often used as the type for primary keys and associations.
The :id
type is used when the primary key is an integer while the
:binary_id
is used for primary keys in particular binary formats,
which may be Ecto.UUID
for databases like PostgreSQL and MySQL,
or some specific ObjectID or RecordID often imposed by NoSQL databases.
In both cases, both types have their semantics specified by the
underlying adapter/database. If you use the :id
type with
:autogenerate
, it means the database will be responsible for
auto-generation of the id. This is often the case for primary keys
in relational databases which are auto-incremented.
Similarly, the :binary_id
type may be generated in the adapter
for cases like UUID but it may also be handled by the database if
required. In any case, both scenarios are handled transparently by
Ecto.
Besides :id
and :binary_id
, which are often used by primary
and foreign keys, Ecto provides a huge variety of types to be used
by any column.
Ecto also supports composite primary keys.
If your primary key is not named “id” (e.g. if you are working with a
legacy database), you can use the @primary_key
attribute to configure
your key name using the source
option. For example, the following
attribute defines an integer primary key named legacy_id
which is
automatically incremented by the database:
@primary_key {:id, :integer, autogenerate: false, source: :legacy_id}
Types and casting
When defining the schema, types need to be given. Types are split into two categories, primitive types and custom types.
Primitive types
The primitive types are:
Ecto type | Elixir type | Literal syntax in query |
---|---|---|
:id | integer | 1, 2, 3 |
:binary_id | binary | <<int, int, int, ...>> |
:integer | integer | 1, 2, 3 |
:float | float | 1.0, 2.0, 3.0 |
:boolean | boolean | true, false |
:string | UTF-8 encoded string | “hello” |
:binary | binary | <<int, int, int, ...>> |
{:array, inner_type} | list | [value, value, value, ...] |
:map | map | |
{:map, inner_type} | map | |
:decimal | Decimal | |
:date | Date | |
:time | Time | |
:naive_datetime | NaiveDateTime | |
:naive_datetime_usec | NaiveDateTime | |
:utc_datetime | DateTime | |
:utc_datetime_usec | DateTime |
Notes:
For the
{:array, inner_type}
and{:map, inner_type}
type, replaceinner_type
with one of the valid types, such as:string
.For the
:decimal
type,+Infinity
,-Infinity
, andNaN
values are not supported, even though theDecimal
library handles them. To support them, you can create a custom type.For calendar types with and without microseconds, the precision is enforced when persisting to the DB. For example, casting
~T[09:00:00]
as:time_usec
will succeed and result in~T[09:00:00.000000]
, but persisting a type without microseconds as:time_usec
will fail. Similarly, casting~T[09:00:00.000000]
as:time
will succeed, but persisting will not. This is the same behaviour as seen in other types, where casting has to be done explicitly and is never performed implicitly when loading from or dumping to the database.
Custom types
Besides providing primitive types, Ecto allows custom types to be implemented by developers, allowing Ecto behaviour to be extended.
A custom type is a module that implements the Ecto.Type
behaviour.
By default, Ecto provides the following custom types:
Custom type | Database type | Elixir type |
---|---|---|
Ecto.UUID | :uuid | uuid-string |
Read the Ecto.Type
documentation for more information on implementing
your own types.
Finally, schemas can also have virtual fields by passing the
virtual: true
option. These fields are not persisted to the database
and can optionally not be type checked by declaring type :any
.
The datetime types
Four different datetime primitive types are available:
naive_datetime
- has a precision of seconds and casts values to Elixir’sNaiveDateTime
struct which has no timezone information.naive_datetime_usec
- has a default precision of microseconds and also casts values toNaiveDateTime
with no timezone information.utc_datetime
- has a precision of seconds and casts values to to Elixir’sDateTime
struct and expects the time zone to be set to UTC.utc_datetime_usec
has a default precision of microseconds and also casts values toDateTime
expecting the time zone be set to UTC.
Having these different types allows developers to choose a type that will be compatible with the database and your project’s precision requirements. For example, some older versions of MySQL do not support microseconds in datetime fields.
When choosing what datetime type to work with, keep in mind that Elixir
functions like NaiveDateTime.utc_now/0
have a default precision of 6.
Casting a value with a precision greater than 0 to a non-usec
type will
truncate all microseconds and set the precision to 0.
The map type
The map type allows developers to store an Elixir map directly in the database:
# In your migration
create table(:users) do
add :data, :map
end
# In your schema
field :data, :map
# Now in your code
user = Repo.insert! %User{data: %{"foo" => "bar"}}
Keep in mind that we advise the map keys to be strings or integers instead of atoms. Atoms may be accepted depending on how maps are serialized but the database will always convert atom keys to strings due to security reasons.
In order to support maps, different databases may employ different techniques. For example, PostgreSQL will store those values in jsonb fields, allowing you to just query parts of it. MySQL and MSSQL, on the other hand, do not yet provide a JSON type, so the value will be stored in a text field.
For maps to work in such databases, Ecto will need a JSON library.
By default Ecto will use Jason
which needs to be added to your deps in mix.exs
:
{:jason, "~> 1.0"}
You can however configure the adapter to use another library. For example, if using Postgres:
config :postgrex, :json_library, YourLibraryOfChoice
Or if using MySQL:
config :mariaex, :json_library, YourLibraryOfChoice
If changing the JSON library, remember to recompile the adapter afterwards by cleaning the current build:
mix deps.clean --build postgrex
Casting
When directly manipulating the struct, it is the responsibility of
the developer to ensure the field values have the proper type. For
example, you can create a user struct with an invalid value
for age
:
iex> user = %User{age: "0"}
iex> user.age
"0"
However, if you attempt to persist the struct above, an error will be raised since Ecto validates the types when sending them to the adapter/database.
Therefore, when working with and manipulating external data, it is
recommended to use Ecto.Changeset
’s that are able to filter
and properly cast external data:
changeset = Ecto.Changeset.cast(%User{}, %{"age" => "0"}, [:age])
user = Repo.insert!(changeset)
You can use Ecto schemas and changesets to cast and validate any kind of data, regardless if the data will be persisted to an Ecto repository or not.
Reflection
Any schema module will generate the __schema__
function that can be
used for runtime introspection of the schema:
__schema__(:source)
- Returns the source as given toschema/2
;__schema__(:prefix)
- Returns optional prefix for source provided by@schema_prefix
schema attribute;__schema__(:primary_key)
- Returns a list of primary key fields (empty if there is none);__schema__(:fields)
- Returns a list of all non-virtual field names;__schema__(:field_source, field)
- Returns the alias of the given field;__schema__(:type, field)
- Returns the type of the given non-virtual field;__schema__(:associations)
- Returns a list of all association field names;__schema__(:association, assoc)
- Returns the association reflection of the given assoc;__schema__(:embeds)
- Returns a list of all embedded field names;__schema__(:embed, embed)
- Returns the embedding reflection of the given embed;__schema__(:read_after_writes)
- Non-virtual fields that must be read back from the database after every write (insert or update);__schema__(:autogenerate_id)
- Primary key that is auto generated on insert;
Furthermore, both __struct__
and __changeset__
functions are
defined so structs and changeset functionalities are available.
Link to this section Summary
Functions
Internal function for integrating associations into schemas
Indicates a one-to-one or many-to-one association with another schema
Defines an embedded schema with the given field definitions
Indicates an embedding of many schemas
Indicates an embedding of many schemas
Indicates an embedding of a schema
Indicates an embedding of a schema
Defines a field on the schema with given name and type
Indicates a one-to-many association with another schema
Indicates a one-to-one association with another schema
Indicates a many-to-many association with another schema
Defines a schema struct with a source name and field definitions
Generates :inserted_at
and :updated_at
timestamp fields
Link to this section Types
schema() :: %atom(){ optional(atom()) => any(), :__meta__ => Ecto.Schema.Metadata.t() }
Link to this section Functions
Internal function for integrating associations into schemas.
This function exists as an extension point for libraries to
add new types of associations to Ecto. For the existing APIs,
see belongs_to/3
, has_many/3
, has_one/3
and many_to_many/3
.
This function expects the current schema, the association cardinality,
the association name, the association module (that implements
Ecto.Association
callbacks) and a keyword list of options.
Indicates a one-to-one or many-to-one association with another schema.
The current schema belongs to zero or one records of the other schema. The other
schema often has a has_one
or a has_many
field with the reverse association.
You should use belongs_to
in the table that contains the foreign key. Imagine
a company <-> employee relationship. If the employee contains the company_id
in
the underlying database table, we say the employee belongs to company.
In fact, when you invoke this macro, a field with the name of foreign key is automatically defined in the schema for you.
Options
:foreign_key
- Sets the foreign key field name, defaults to the name of the association suffixed by_id
. For example,belongs_to :company
will define foreign key of:company_id
:references
- Sets the key on the other schema to be used for the association, defaults to::id
:define_field
- When false, does not automatically define a:foreign_key
field, implying the user is defining the field manually elsewhere:type
- Sets the type of automatically defined:foreign_key
. Defaults to::integer
and can be set per schema via@foreign_key_type
:on_replace
- The action taken on associations when the record is replaced when casting or manipulating parent changeset. May be:raise
(default),:mark_as_invalid
,:nilify
,:update
, or:delete
. SeeEcto.Changeset
’s section on related data for more info.:defaults
- Default values to use when building the association:primary_key
- If the underlying belongs_to field is a primary key:source
- The source for the underlying field:where
- A filter for the association. See “Filtering associations” inhas_many/3
.
Examples
defmodule Comment do
use Ecto.Schema
schema "comments" do
belongs_to :post, Post
end
end
# The post can come preloaded on the comment record
[comment] = Repo.all(from(c in Comment, where: c.id == 42, preload: :post))
comment.post #=> %Post{...}
If you need custom options on the underlying field, you can define the
field explicitly and then pass define_field: false
to belongs_to
:
defmodule Comment do
use Ecto.Schema
schema "comments" do
field :post_id, :integer, ... # custom options
belongs_to :post, Post, define_field: false
end
end
Using Queries as Associations
A query can also be given instead of a schema. Querying, joining or preloading the association will use the given query. Currently only where clauses can be provided in queries. Let’s see an example:
defmodule Post do
...
def active() do
from post in Post,
where: post.active
end
end
defmodule Comment do
use Ecto.Schema
schema "posts" do
belongs_to :post, Post.active()
end
end
Note: building the association does not consider the query filters. For example, if the given query requires the active field of the associated records to be true, building such association won’t automatically set the active field to true.
Polymorphic associations
One common use case for belongs to associations is to handle polymorphism. For example, imagine you have defined a Comment schema and you wish to use it for commenting on both tasks and posts.
Some abstractions would force you to define some sort of polymorphic association with two fields in your database:
* commentable_type
* commentable_id
The problem with this approach is that it breaks references in the database. You can’t use foreign keys and it is very inefficient, both in terms of query time and storage.
In Ecto, we have three ways to solve this issue. The simplest is to define multiple fields in the Comment schema, one for each association:
* task_id
* post_id
Unless you have dozens of columns, this is simpler for the developer, more DB friendly and more efficient in all aspects.
Alternatively, because Ecto does not tie a schema to a given table, we can use separate tables for each association. Let’s start over and define a new Comment schema:
defmodule Comment do
use Ecto.Schema
schema "abstract table: comments" do
# This will be used by associations on each "concrete" table
field :assoc_id, :integer
end
end
Notice we have changed the table name to “abstract table: comments”. You can choose whatever name you want, the point here is that this particular table will never exist.
Now in your Post and Task schemas:
defmodule Post do
use Ecto.Schema
schema "posts" do
has_many :comments, {"posts_comments", Comment}, foreign_key: :assoc_id
end
end
defmodule Task do
use Ecto.Schema
schema "tasks" do
has_many :comments, {"tasks_comments", Comment}, foreign_key: :assoc_id
end
end
Now each association uses its own specific table, “posts_comments” and “tasks_comments”, which must be created on migrations. The advantage of this approach is that we never store unrelated data together, also ensuring we keep database references fast and correct.
When using this technique, the only limitation is that you cannot build comments directly. For example, the command below
Repo.insert!(%Comment{})
will attempt to use the abstract table. Instead, one should use
Repo.insert!(build_assoc(post, :comments))
leveraging the Ecto.build_assoc/3
function. You can also
use Ecto.assoc/2
and Ecto.Query.assoc/2
in the query syntax
to easily retrieve associated comments to a given post or
task:
# Fetch all comments associated with the given task
Repo.all(assoc(task, :comments))
Or all comments in a given table:
Repo.all from(c in {"posts_comments", Comment}), ...)
The third and final option is to use many_to_many/3
to
define the relationships between the resources. In this case,
the comments table won’t have the foreign key, instead there
is a intermediary table responsible for associating the entries:
defmodule Comment do
use Ecto.Schema
schema "comments" do
# ...
end
end
In your posts and tasks:
defmodule Post do
use Ecto.Schema
schema "posts" do
many_to_many :comments, Comment, join_through: "posts_comments"
end
end
defmodule Task do
use Ecto.Schema
schema "tasks" do
many_to_many :comments, Comment, join_through: "tasks_comments"
end
end
See many_to_many/3
for more information on this particular approach.
Defines an embedded schema with the given field definitions.
An embedded schema is either embedded into another schema or kept exclusively in memory. For this reason, an embedded schema does not require a source name and it does not include a metadata field.
Embedded schemas by default set the primary key type
to :binary_id
but such can be configured with the
@primary_key
attribute.
Indicates an embedding of many schemas.
The current schema has zero or more records of the other schema embedded inside of it. Embeds have all the things regular schemas have.
It is recommended to declare your embeds_many/3
field with type :map
and a default of "[]"
(although Ecto will also automatically translate
nil
values from the database into empty lists).
The embedded may or may not have a primary key. Ecto use the primary keys
to detect if an embed is being updated or not. If a primary is not present
and you still want the list of embeds to be updated, :on_replace
must be
set to :delete
, forcing all current embeds to be deleted and replaced by
new ones whenever a new list of embeds is set.
For encoding and decoding of embeds, please read the docs for
embeds_one/3
.
Options
:on_replace
- The action taken on associations when the embed is replaced when casting or manipulating parent changeset. May be:raise
(default),:mark_as_invalid
, or:delete
. SeeEcto.Changeset
’s section on related data for more info.
Examples
defmodule Order do
use Ecto.Schema
schema "orders" do
embeds_many :items, Item
end
end
defmodule Item do
use Ecto.Schema
embedded_schema do
field :title
end
end
# The items are loaded with the order
order = Repo.get!(Order, 42)
order.items #=> [%Item{...}, ...]
Adding and removal of embeds can only be done via the Ecto.Changeset
API so Ecto can properly track the embed life-cycle:
# Order has no items
order = Repo.get!(Order, 42)
order.items
# => []
items = [%Item{title: "Soap"}]
# Generate a changeset
changeset = Ecto.Changeset.change(order)
# Put a one or more new items
changeset = Ecto.Changeset.put_embed(changeset, :items, items)
# Update the order and fetch items
items = Repo.update!(changeset).items
# Items are generated with a unique identification
items
# => [%Item{id: "20a97d94-f79b-4e63-a875-85deed7719b7", title: "Soap"}]
Updating of embeds must be done using a changeset for each changed embed.
# Order has an existing items
order = Repo.get!(Order, 42)
order.items
# => [%Item{id: "20a97d94-f79b-4e63-a875-85deed7719b7", title: "Soap"}]
# Generate a changeset
changeset = Ecto.Changeset.change(order)
# Put the updated item as a changeset
current_item = List.first(order.items)
item_changeset = Ecto.Changeset.change(current_item, title: "Mujju's Soap")
order_changeset = Ecto.Changeset.put_embed(changeset, :items, [item_changeset])
# Update the order and fetch items
items = Repo.update!(order_changeset).items
# Item has the updated title
items
# => [%Item{id: "20a97d94-f79b-4e63-a875-85deed7719b7", title: "Mujju's Soap"}]
Inline embedded schema
The schema module can be defined inline in the parent schema in simple cases:
defmodule Parent do
use Ecto.Schema
schema "parents" do
field :name, :string
embeds_many :children, Child do
field :name, :string
field :age, :integer
end
end
end
When defining an inline embed, the :primary_key
option may be given to
customize the embed primary key type.
Defining embedded schema in such a way will define a Parent.Child
module
with the appropriate struct. In order to properly cast the embedded schema.
When casting the inline-defined embedded schemas you need to use the :with
option of cast_embed/3
to provide the proper function to do the casting.
For example:
def changeset(schema, params) do
schema
|> cast(params, [:name])
|> cast_embed(:children, with: &child_changeset/2)
end
defp child_changeset(schema, params) do
schema
|> cast(params, [:name, :age])
end
Indicates an embedding of many schemas.
For options and examples see documentation of embeds_many/3
.
Indicates an embedding of a schema.
The current schema has zero or one records of the other schema embedded
inside of it. It uses a field similar to the :map
type for storage,
but allows embeds to have all the things regular schema can.
You must declare your embeds_one/3
field with type :map
at the
database level.
The embedded may or may not have a primary key. Ecto use the primary keys
to detect if an embed is being updated or not. If a primary is not present,
:on_replace
should be set to either :update
or :delete
if there is a
desire to either update or delete the current embed when a new one is set.
Options
:on_replace
- The action taken on associations when the embed is replaced when casting or manipulating parent changeset. May be:raise
(default),:mark_as_invalid
,:update
, or:delete
. SeeEcto.Changeset
’s section on related data for more info.
Examples
defmodule Order do
use Ecto.Schema
schema "orders" do
embeds_one :item, Item
end
end
defmodule Item do
use Ecto.Schema
embedded_schema do
field :title
end
end
# The item is loaded with the order
order = Repo.get!(Order, 42)
order.item #=> %Item{...}
Adding and removal of embeds can only be done via the Ecto.Changeset
API so Ecto can properly track the embed life-cycle:
order = Repo.get!(Order, 42)
item = %Item{title: "Soap"}
# Generate a changeset
changeset = Ecto.Changeset.change(order)
# Put a new embed to the changeset
changeset = Ecto.Changeset.put_embed(changeset, :item, item)
# Update the order, and fetch the item
item = Repo.update!(changeset).item
# Item is generated with a unique identification
item
# => %Item{id: "20a97d94-f79b-4e63-a875-85deed7719b7", title: "Soap"}
Inline embedded schema
The schema module can be defined inline in the parent schema in simple cases:
defmodule Parent do
use Ecto.Schema
schema "parents" do
field :name, :string
embeds_one :child, Child do
field :name, :string
field :age, :integer
end
end
end
When defining an inline embed, the :primary_key
option may be given to
customize the embed primary key type.
Options should be passed before the do
block like this:
embeds_one :child, Child, on_replace: :delete do
field :name, :string
field :age, :integer
end
Defining embedded schema in such a way will define a Parent.Child
module
with the appropriate struct. In order to properly cast the embedded schema.
When casting the inline-defined embedded schemas you need to use the :with
option of Ecto.Changeset.cast_embed/3
to provide the proper function to do the casting.
For example:
def changeset(schema, params) do
schema
|> cast(params, [:name])
|> cast_embed(:child, with: &child_changeset/2)
end
defp child_changeset(schema, params) do
schema
|> cast(params, [:name, :age])
end
Encoding and decoding
Because many databases do not support direct encoding and decoding of embeds, it is often emulated by Ecto by using specific encoding and decoding rules.
For example, PostgreSQL will store embeds on top of JSONB columns, which means types in embedded schemas won’t go through the usual dump->DB->load cycle but rather encode->DB->decode->cast. This means that, when using embedded schemas with databases like PG or MySQL, make sure all of your types can be JSON encoded/decoded correctly. Ecto provides this guarantee for all built-in types.
Indicates an embedding of a schema.
For options and examples see documentation of embeds_one/3
.
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.: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
Indicates a one-to-many association with another schema.
The current schema has zero or more records of the other schema. The other
schema often has a belongs_to
field with the reverse association.
Options
:foreign_key
- Sets the foreign key, this should map to a field on the other schema, defaults to the underscored name of the current schema suffixed by_id
:references
- Sets the key on the current schema to be used for the association, defaults to the primary key on the schema:through
- Allow this association to be defined in terms of existing associations. Read the section on:through
associations for more info:on_delete
- The action taken on associations when parent record is deleted. May be:nothing
(default),:nilify_all
and:delete_all
. Using this option is DISCOURAGED for most relational databases. Instead, in your migration, setreferences(:parent_id, on_delete: :delete_all)
. Opposite to the migration option, this option cannot guarantee integrity and it is only triggered forEcto.Repo.delete/2
(and not onEcto.Repo.delete_all/2
) and it never cascades. If posts has many comments, which has many tags, and you delete a post, only comments will be deleted. If your database does not support references, cascading can be manually implemented by usingEcto.Multi
orEcto.Changeset.prepare_changes/2
.:on_replace
- The action taken on associations when the record is replaced when casting or manipulating parent changeset. May be:raise
(default),:mark_as_invalid
,:nilify
, or:delete
. SeeEcto.Changeset
’s section on related data for more info.:defaults
- Default values to use when building the association:where
- A filter for the association. See “Filtering associations” below. It does not apply to:through
associations.
Examples
defmodule Post do
use Ecto.Schema
schema "posts" do
has_many :comments, Comment
end
end
# Get all comments for a given post
post = Repo.get(Post, 42)
comments = Repo.all assoc(post, :comments)
# The comments can come preloaded on the post struct
[post] = Repo.all(from(p in Post, where: p.id == 42, preload: :comments))
post.comments #=> [%Comment{...}, ...]
has_many
can be used to define hierarchical relationships within a single
schema, for example threaded comments.
defmodule Comment do
use Ecto.Schema
schema "comments" do
field :content, :string
field :parent_id, :integer
belongs_to :parent, Comment, foreign_key: :id, references: :parent_id, define_field: false
has_many :children, Comment, foreign_key: :parent_id, references: :id
end
end
Filtering associations
A query can also be given instead of a schema. Querying, joining or preloading the association will use the given query. Currently only where clauses can be provided in queries. Let’s see some examples:
defmodule Post do
use Ecto.Schema
schema "posts" do
has_many :public_comments, Comment,
where: [public: true]
has_many :deleted_comments, Comment,
where: dynamic([c], not(is_nil(comment.deleted_at)))
end
end
The :where
option may receive a dynamic query, a keyword list or a MFA
(a tuple with a module, function and args to invoke). The MFA is especially
useful to avoid duplication in those definitions:
defmodule Comment do
def deleted_filter do
dynamic([c], not(is_nil(comment.deleted_at)))
end
end
defmodule Post do
use Ecto.Schema
schema "posts" do
has_many :deleted_comments, Comment,
where: {Comment, :deleted_filter, []}
end
end
Important! Please use this feature only when strictly necessary, otherwise it is very easy to end-up with large schemas with dozens of different associations polluting your schema and affecting your application performance. For instance, if you are using associations only for different querying purposes, then it is preferable to build and compose queries, rather than defining multiple associations:
posts
|> Ecto.assoc(:comments)
|> Comment.deleted()
Or when preloading:
from posts, preload: [comments: ^Comment.deleted()]
has_many/has_one :through
Ecto also supports defining associations in terms of other associations
via the :through
option. Let’s see an example:
defmodule Post do
use Ecto.Schema
schema "posts" do
has_many :comments, Comment
has_one :permalink, Permalink
# In the has_many :through example below, the `:comments`
# in the list [:comments, :author] refers to the
# `has_many :comments` in the Post own schema and the
# `:author` refers to the `belongs_to :author` of the
# Comment's schema (the module below).
# (see the description below for more details)
has_many :comments_authors, through: [:comments, :author]
# Specify the association with custom source
has_many :tags, {"posts_tags", Tag}
end
end
defmodule Comment do
use Ecto.Schema
schema "comments" do
belongs_to :author, Author
belongs_to :post, Post
has_one :post_permalink, through: [:post, :permalink]
end
end
In the example above, we have defined a has_many :through
association
named :comments_authors
. A :through
association always expects a list
and the first element of the list must be a previously defined association
in the current module. For example, :comments_authors
first points to
:comments
in the same module (Post), which then points to :author
in
the next schema, Comment
.
This :through
association will return all authors for all comments
that belongs to that post:
# Get all comments authors for a given post
post = Repo.get(Post, 42)
authors = Repo.all assoc(post, :comments_authors)
Although we used the :through
association in the example above, Ecto
also allows developers to dynamically build the through associations using
the Ecto.assoc/2
function:
assoc(post, [:comments, :author])
In fact, given :through
associations are read-only, using the Ecto.assoc/2
format is the preferred mechanism for working with through associations. Use
the schema-based one only if you need to store the through data alongside of
the parent struct, in specific cases such as preloading.
:through
associations can also be preloaded. In such cases, not only
the :through
association is preloaded but all intermediate steps are
preloaded too:
[post] = Repo.all(from(p in Post, where: p.id == 42, preload: :comments_authors))
post.comments_authors #=> [%Author{...}, ...]
# The comments for each post will be preloaded too
post.comments #=> [%Comment{...}, ...]
# And the author for each comment too
hd(post.comments).author #=> %Author{...}
When the :through
association is expected to return one or zero items,
has_one :through
should be used instead, as in the example at the beginning
of this section:
# How we defined the association above
has_one :post_permalink, through: [:post, :permalink]
# Get a preloaded comment
[comment] = Repo.all(Comment) |> Repo.preload(:post_permalink)
comment.post_permalink #=> %Permalink{...}
Indicates a one-to-one association with another schema.
The current schema has zero or one records of the other schema. The other
schema often has a belongs_to
field with the reverse association.
Options
:foreign_key
- Sets the foreign key, this should map to a field on the other schema, defaults to the underscored name of the current module suffixed by_id
:references
- Sets the key on the current schema to be used for the association, defaults to the primary key on the schema:through
- If this association must be defined in terms of existing associations. Read the section inhas_many/3
for more information:on_delete
- The action taken on associations when parent record is deleted. May be:nothing
(default),:nilify_all
and:delete_all
. Using this option is DISCOURAGED for most relational databases. Instead, in your migration, setreferences(:parent_id, on_delete: :delete_all)
. Opposite to the migration option, this option cannot guarantee integrity and it is only triggered forEcto.Repo.delete/2
(and not onEcto.Repo.delete_all/2
) and it never cascades. If posts has many comments, which has many tags, and you delete a post, only comments will be deleted. If your database does not support references, cascading can be manually implemented by usingEcto.Multi
orEcto.Changeset.prepare_changes/2
:on_replace
- The action taken on associations when the record is replaced when casting or manipulating parent changeset. May be:raise
(default),:mark_as_invalid
,:nilify
,:update
, or:delete
. SeeEcto.Changeset
’s section on related data for more info.:defaults
- Default values to use when building the association:where
- A filter for the association. See “Filtering associations” inhas_many/3
. It does not apply to:through
associations.
Examples
defmodule Post do
use Ecto.Schema
schema "posts" do
has_one :permalink, Permalink
# Specify the association with custom source
has_one :category, {"posts_categories", Category}
end
end
# The permalink can come preloaded on the post struct
[post] = Repo.all(from(p in Post, where: p.id == 42, preload: :permalink))
post.permalink #=> %Permalink{...}
Using Queries as Associations
A query can also be given instead of a schema. Querying, joining or preloading the association will use the given query. Currently only where clauses can be provided in queries. Let’s see an example:
defmodule Post do
...
def active() do
from post in Post,
where: post.active
end
end
defmodule Comment do
use Ecto.Schema
schema "comments" do
has_one :post, Post.active()
end
end
Note: building the association does not consider the query filters. For example, if the given query requires the active field of the associated records to be true, building such association won’t automatically set the active field to true.
Indicates a many-to-many association with another schema.
The association happens through a join schema or source, containing foreign keys to the associated schemas. For example, the association below:
# from MyApp.Post
many_to_many :tags, MyApp.Tag, join_through: "posts_tags"
is backed by relational databases through a join table as follows:
[Post] <-> [posts_tags] <-> [Tag]
id <-- post_id
tag_id --> id
More information on the migration for creating such a schema is shown below.
Options
:join_through
- specifies the source of the associated data. It may be a string, like “posts_tags”, representing the underlying storage table or an atom, likeMyApp.PostTag
, representing a schema. This option is required.:join_keys
- specifies how the schemas are associated. It expects a keyword list with two entries, the first being how the join table should reach the current schema and the second how the join table should reach the associated schema. In the example above, it defaults to:[post_id: :id, tag_id: :id]
. The keys are inflected from the schema names.:on_delete
- The action taken on associations when the parent record is deleted. May be:nothing
(default) or:delete_all
. Using this option is DISCOURAGED for most relational databases. Instead, in your migration, setreferences(:parent_id, on_delete: :delete_all)
. Opposite to the migration option, this option cannot guarantee integrity and it is only triggered forEcto.Repo.delete/2
(and not onEcto.Repo.delete_all/2
). This option can only remove data from the join source, never the associated records, and it never cascades.:on_replace
- The action taken on associations when the record is replaced when casting or manipulating parent changeset. May be:raise
(default),:mark_as_invalid
, or:delete
.:delete
will only remove data from the join source, never the associated records. SeeEcto.Changeset
’s section on related data for more info.:defaults
- Default values to use when building the association:unique
- When true, checks if the associated entries are unique. This is done by checking the primary key of the associated entries during repository operations. Keep in mind this does not guarantee uniqueness at the database level. For such it is preferred to set a unique index in the database. For example:create unique_index(:posts_tags, [:post_id, :tag_id])
:where
- A filter for the association. See “Filtering associations” inhas_many/3
:join_through_where
- A filter for the join through association. See “Filtering associations” inhas_many/3
Removing data
If you attempt to remove associated many_to_many
data, Ecto will
always remove data from the join schema and never from the target
associations be it by setting :on_replace
to :delete
, :on_delete
to :delete_all
or by using changeset functions such as
Ecto.Changeset.put_assoc/3
. For example, if a Post
has a many to many
relationship with Tag
, setting :on_delete
to :delete_all
will
only delete entries from the “posts_tags” table in case Post
is
deleted.
Migration
How your migration should be structured depends on the value you pass
in :join_through
. If :join_through
is simply a string, representing
a table, you may define a table without primary keys and you must not
include any further columns, as those values won’t be set by Ecto:
create table(:posts_tags, primary_key: false) do
add :post_id, references(:posts)
add :tag_id, references(:tags)
end
However, if your :join_through
is a schema, like MyApp.PostTag
, your
join table may be structured as any other table in your codebase,
including timestamps:
create table(:posts_tags) do
add :post_id, references(:posts)
add :tag_id, references(:tags)
timestamps()
end
Because :join_through
contains a schema, in such cases, autogenerated
values and primary keys will be automatically handled by Ecto.
Examples
defmodule Post do
use Ecto.Schema
schema "posts" do
many_to_many :tags, Tag, join_through: "posts_tags"
end
end
# Let's create a post and a tag
post = Repo.insert!(%Post{})
tag = Repo.insert!(%Tag{name: "introduction"})
# We can associate at any time post and tags together using changesets
post
|> Repo.preload(:tags) # Load existing data
|> Ecto.Changeset.change() # Build the changeset
|> Ecto.Changeset.put_assoc(:tags, [tag]) # Set the association
|> Repo.update!
# In a later moment, we may get all tags for a given post
post = Repo.get(Post, 42)
tags = Repo.all(assoc(post, :tags))
# The tags may also be preloaded on the post struct for reading
[post] = Repo.all(from(p in Post, where: p.id == 42, preload: :tags))
post.tags #=> [%Tag{...}, ...]
Join Schema Example
You may prefer to use a join schema to handle many_to_many associations. The
decoupled nature of Ecto allows us to create a “join” struct which
belongs_to
both sides of the many to many association.
In our example, a User has and belongs to many Organizations
defmodule UserOrganization do
use Ecto.Schema
@primary_key false
schema "users_organizations" do
belongs_to :user, User
belongs_to :organization, Organization
timestamps() # Added bonus, a join schema will also allow you to set timestamps
end
def changeset(struct, params \\ %{}) do
struct
|> Ecto.Changeset.cast(params, [:user_id, :organization_id])
|> Ecto.Changeset.validate_required([:user_id, :organization_id])
# Maybe do some counter caching here!
end
end
defmodule User do
use Ecto.Schema
schema "users" do
many_to_many :organizations, Organization, join_through: UserOrganization
end
end
defmodule Organization do
use Ecto.Schema
schema "organizations" do
many_to_many :users, User, join_through: UserOrganization
end
end
# Then to create the association, pass in the ID's of an existing
# User and Organization to UserOrganization.changeset
changeset = UserOrganization.changeset(%UserOrganization{}, %{user_id: id, organization_id: id})
case Repo.insert(changeset) do
{:ok, assoc} -> # Assoc was created!
{:error, changeset} -> # Handle the error
end
Using Queries as Associations
A query can also be given instead of a schema, both for the join_through and the destination. Querying, joining or preloading the association will use the given query. Currently only where clauses can be provided in queries. Let’s see an example:
defmodule UserOrganization do
use Ecto.Schema
@primary_key false
schema "users_organizations" do
belongs_to :user, User
belongs_to :organization, Organization
field :deleted, :boolean
timestamps() # Added bonus, a join schema will also allow you to set timestamps
end
def active() do
from user_organization in UserOrganization,
where: is_nil(user_organization.deleted)
end
end
defmodule User do
use Ecto.Schema
schema "users" do
many_to_many :organizations, Organization, join_through: UserOrganization
field :banned, :boolean
end
def not_banned() do
from user in User,
where: not(user.banned)
end
end
defmodule Organization do
use Ecto.Schema
schema "organizations" do
many_to_many :users, User.not_banned(), join_through: UserOrganization.active()
end
end
Note: building the association does not consider the query filters. For example, if the given query requires the active field of the associated records to be true, building such association won’t automatically set the active field to true.
Defines a schema struct with a source name and field definitions.
An additional field called __meta__
is added to the struct for storing
internal Ecto state. This field always has a Ecto.Schema.Metadata
struct
as value and can be manipulated with the Ecto.put_meta/2
function.
Generates :inserted_at
and :updated_at
timestamp fields.
The fields generated by this macro will automatically be set to the current time when inserting and updating values in a repository.
Options
:type
- the timestamps type, defaults to:naive_datetime
.:inserted_at
- the name of the column for insertion times orfalse
:updated_at
- the name of the column for update times orfalse
:autogenerate
- a module-function-args tuple used for generating bothinserted_at
andupdated_at
timestamps
All options can be pre-configured by setting @timestamps_opts
.