Ecto.Schema

Ecto.Schema

Defines a schema.

An Ecto schema is used to map any data source into an Elixir struct. One of such use cases is to map data coming from a repository, usually a table, into Elixir structs.

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.

Schema attributes

Supported attributes, to be set beforehand