Use when defining data structures using Ecto schemas including fields, associations, embedded schemas, and schema metadata. Use for modeling domain data in Elixir applications.
View on GitHubTheBushidoCollective/han
jutsu-ecto
January 24, 2026
Select agents to install to:
npx add-skill https://github.com/TheBushidoCollective/han/blob/main/jutsu/jutsu-ecto/skills/ecto-schema-patterns/SKILL.md -a claude-code --skill ecto-schema-patternsInstallation paths:
.claude/skills/ecto-schema-patterns/# Ecto Schema Patterns
Master Ecto schemas to define robust data structures for your Elixir applications.
This skill covers schema definitions, field types, associations, embedded schemas,
and advanced patterns for modeling complex domain data.
## Basic Schema Definition
```elixir
defmodule MyApp.User do
use Ecto.Schema
schema "users" do
field :name, :string
field :email, :string
field :age, :integer
field :confirmed_at, :naive_datetime
timestamps()
end
end
```
Schemas map to database tables and define the structure of your data. Each schema
has a source name (table name) and a list of fields with their types. The `timestamps()`
macro automatically adds `inserted_at` and `updated_at` fields.
## Field Types and Options
```elixir
defmodule MyApp.Product do
use Ecto.Schema
schema "products" do
# Standard field types
field :title, :string
field :description, :string
field :price, :decimal
field :quantity, :integer
field :is_active, :boolean, default: true
field :published_at, :utc_datetime
# Enum type
field :status, Ecto.Enum, values: [:draft, :published, :archived]
# Map type for unstructured data
field :metadata, :map
# Array type
field :tags, {:array, :string}
# Binary type for binary data
field :image_data, :binary
# Virtual field (not persisted to database)
field :display_price, :string, virtual: true
timestamps()
end
end
```
Ecto supports a wide range of field types including strings, integers, decimals,
booleans, datetime types, enums, maps, arrays, and binary data. Virtual fields
exist only in memory and are useful for computed values.
## Using Map Type for Flexible Data
```elixir
defmodule MyApp.User do
use Ecto.Schema
schema "users" do
field :name, :string
field :email, :string
field :data, :map
timestamps()
end
end
# Usage
user = %MyApp.User{
name: "John Doe",
email: "john@example.com",
data: %{
preferenc