本文へ移動
cccskills
無料GitHub で公開

ecto-patterns

Use when a task touches Ecto, even a how-to question: schemas, changesets, validations, queries, preloads, Multi, migrations, constraints, money fields. Load it before writing Ecto code. Skip for Ash.

インストール方法を見る

含まれるファイル(6)

  • SKILL.md3.6 KB
  • references/changesets.md4.2 KB
  • references/fulltext-search.md5.6 KB
  • references/migrations.md4.5 KB
  • references/queries.md4.9 KB
  • references/transactions.md4.4 KB

SKILL.md(原文)

インストールする前に、エージェントに与えられる指示の中身を確認できます。

Ecto Patterns Reference

Reference for working with Ecto schemas, queries, and migrations.

Iron Laws — Never Violate These

  1. CHANGESETS ARE FOR EXTERNAL DATA — Use cast/4 for user/API input, change/2 or put_change/3 for internal trusted data
  2. NEVER USE :float FOR MONEY — Always use :decimal or :integer (cents)
  3. NO RAILS-STYLE POLYMORPHIC ASSOCIATIONS — They break foreign key constraints; use multiple nullable FKs or separate join tables
  4. ALWAYS PIN VALUES IN QUERIES — u.name == ^user_input is safe, string interpolation causes SQL injection
  5. PRELOAD COLLECTIONS, NOT INDIVIDUALS — Preloading in loops = N+1 queries
  6. CONSTRAINTS BEAT VALIDATIONS FOR RACE CONDITIONS — Validations provide quick feedback, constraints provide DB-level safety
  7. SEPARATE QUERIES FOR has_many, JOIN FOR belongs_to — Avoids row multiplication
  8. NO IMPLICIT CROSS JOINS — from(a in A, b in B) without on: creates Cartesian product
  9. DEDUP BEFORE cast_assoc WITH SHARED DATA — When multiple parents share child data, deduplicate child records BEFORE building changesets. Dedup only works within a single changeset

Quick Schema Template

defmodule MyApp.Context.Entity do
  use Ecto.Schema
  import Ecto.Changeset

  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id

  schema "entities" do
    field :name, :string
    field :status, Ecto.Enum, values: [:draft, :active, :archived]
    field :amount_cents, :integer  # Never :float for money!
    belongs_to :user, MyApp.Accounts.User
    timestamps(type: :utc_datetime_usec)
  end

  def changeset(entity, attrs) do
    entity
    |> cast(attrs, [:name, :status, :amount_cents])
    |> validate_required([:name])
    |> foreign_key_constraint(:user_id)
  end
end

Quick Decisions

cast vs put_change vs change

FunctionUse When
cast/4External data (user input, API)
put_change/3Internal trusted data (timestamps, computed)
change/2Internal data from existing struct

Preload Strategy

RelationshipStrategy
belongs_toJOIN (single query)
has_manySeparate queries (avoid row multiplication)

Common Anti-patterns

WrongRight
field :amount, :floatfield :amount_cents, :integer
"SELECT * WHERE name = '#{name}'"from(u in User, where: u.name == ^name)
Repo.all(User) |> Enum.filter(& &1.active)from(u in User, where: u.active)
Preloading in loopsRepo.preload(posts, :comments)
Repo.get!(User, user_id) with user inputRepo.get(User, id) + handle nil
{:ok, _} = Repo.update(cs) inside Repo.transactioncase/with + Repo.rollback(cs) (see transactions.md)

References

For detailed patterns, see:

  • ${CLAUDE_SKILL_DIR}/references/changesets.md - cast vs put_change, custom validations, prepare_changes
  • ${CLAUDE_SKILL_DIR}/references/queries.md - Composable queries, dynamic, subqueries, preloading
  • ${CLAUDE_SKILL_DIR}/references/migrations.md - Safe migrations, concurrent indexes, NOT NULL
  • ${CLAUDE_SKILL_DIR}/references/transactions.md - Repo.transact, Ecto.Multi, upserts

レビュー

まだレビューはありません。使ってみた感想をお寄せください。

同じリポジトリのスキル

概要と使いどころ

Ash Framework — resources, actions, policies, aggregates; Use when generating resources via mix ash.codegen, editing…

日本語の概要は準備中です。原文の説明を表示しています。

oliver-kriska/claude-elixir-phoenix5652026年10月6日 更新

Ash Framework — resources, actions, policies, aggregates, calculations, AshPhoenix.Form, LiveView, migrations. Use when generating resources via mix ash.codegen, editing changes, checks, types, validations, or domain code interfaces.

日本語の概要は準備中です。原文の説明を表示しています。

oliver-kriska/claude-elixir-phoenix5652026年10月6日 更新

assigns

無料

Compatibility alias for the Elixir/Phoenix plugin's LiveView assigns audit. Invoke explicitly with /lv:assigns.

日本語の概要は準備中です。原文の説明を表示しています。

oliver-kriska/claude-elixir-phoenix5652026年10月6日 更新

Inspect LiveView socket assigns for memory bloat — missing temporary_assigns, unused assigns, unbounded lists needing streams, memory estimates. Use when LiveView memory grows or you need to add temporary_assigns.

日本語の概要は準備中です。原文の説明を表示しています。

oliver-kriska/claude-elixir-phoenix5652026年10月6日 更新

audit

無料

Project health audit and health check — architecture, performance, tests, dependencies, code quality. Use when assessing overall project health, before releases, or after refactors.

日本語の概要は準備中です。原文の説明を表示しています。

oliver-kriska/claude-elixir-phoenix5652026年10月6日 更新

Analyze Phoenix context boundaries and module coupling via mix xref. Use when checking cross-context calls, validating dependencies, before splitting modules, or reviewing architecture.

日本語の概要は準備中です。原文の説明を表示しています。

oliver-kriska/claude-elixir-phoenix5652026年10月6日 更新

oliver-kriska のスキルをすべて見る

このスキルの問題を報告する