This skill should be used when the user asks to "add a database table", "create a new context", "query the database", "add a field to a schema", "validate form input", "fix N+1 queries", "preload this association", "separate these concerns", or mentions Repo, changesets, migrations, Ecto.Multi, has_many, belongs_to, transactions, query composition, or how contexts should talk to each other.
View on GitHubgeorgeguimaraes/claude-code-elixir
elixir
January 23, 2026
Select agents to install to:
npx add-skill https://github.com/georgeguimaraes/claude-code-elixir/blob/main/plugins/elixir/skills/ecto-thinking/SKILL.md -a claude-code --skill ecto-thinkingInstallation paths:
.claude/skills/ecto-thinking/# Ecto Thinking Mental shifts for Ecto and data layer design. These insights challenge typical ORM patterns. ## Context = Setting That Changes Meaning Context isn't just a namespace—it changes what words mean. "Product" means different things in Checkout (SKU, name), Billing (SKU, cost), and Fulfillment (SKU, warehouse). Each bounded context may have its OWN Product schema/table. **Think top-down:** Subdomain → Context → Entity. Not "What context does Product belong to?" but "What is a Product in this business domain?" ## Cross-Context References: IDs, Not Associations ```elixir schema "cart_items" do field :product_id, :integer # Reference by ID # NOT: belongs_to :product, Catalog.Product end ``` Query through the context, not across associations. Keeps contexts independent and testable. ## DDD Patterns as Pipelines ```elixir def create_product(params) do params |> Products.build() # Factory: unstructured → domain |> Products.validate() # Aggregate: enforce invariants |> Products.insert() # Repository: persist end ``` Use events (as data structs) to compose bounded contexts with minimal coupling. ## Schema ≠ Database Table | Use Case | Approach | |----------|----------| | Database table | Standard `schema/2` | | Form validation only | `embedded_schema/1` | | API request/response | Embedded schema or schemaless | ## Multiple Changesets per Schema ```elixir def registration_changeset(user, attrs) # Full validation + password def profile_changeset(user, attrs) # Name, bio only def admin_changeset(user, attrs) # Role, verified_at ``` Different operations = different changesets. ## Multi-Tenancy: Composite Foreign Keys ```elixir add :post_id, references(:posts, with: [org_id: :org_id], match: :full) ``` Use `prepare_query/3` for automatic scoping. Raise if `org_id` missing. ## Preload vs Join Trade-offs | Approach | Best For | |----------|----------| | Separate preloads | Has-many with many records (less memo