ecto-patterns
/ecto-patterns
Podpowiada wzorce pracy z Ecto: schematy, changesety, walidacje, zapytania, preloady i Multi, ładowane przed pisaniem kodu Ecto.
- Autor
- oliver-kriska ↗
- Repozytorium
- claude-elixir-phoenix ↗
- Licencja
- MIT
- Ostatnia zmiana repozytorium
- 22.10.2026
Ocena i bezpieczeństwo
- Pliki w paczce
- 6
- Skrypty
- 0
- Komentarze HTML
- 0
- Adresy URL w SKILL.md
- 0
Brak sygnałów. Repozytorium zmienione 22.10.2026, bez odwołań do wycofanych modeli i API.
MIT, otwarta: można używać i zmieniać, z zachowaniem informacji o autorze.
Co robi i kiedy
Podpowiada wzorce pracy z Ecto: schematy, changesety, walidacje, zapytania, preloady i Multi, ładowane przed pisaniem kodu Ecto.
Oryginalny opis (EN)
Use when a task touches Ecto, even a how-to question: schemas, changesets, validations, queries, preloads, Multi…
Jak działa
- Iron Laws — Never Violate These
- Quick Schema Template
- Quick Decisions
- Common Anti-patterns
- References
Podgląd SKILL.md
---
name: ecto-patterns
description: 'Use when a task touches Ecto, even a how-to question: schemas, changesets,
validations, queries, preloads, Multi…'
---
# 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
```elixir
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
| Function | Use When |
|----------|----------|
| `cast/4` | External data (user input, API) |
| `put_change/3` | Internal trusted data (timestamps, computed) |
| `change/2` | Internal data from existing struct |
### Preload Strategy
| Relationship | Strategy |
|--------------|----------|
| `belongs_to` | JOIN (single query) |
| `has_many` | Separate queries (avoid row multiplication) |
## Common Anti-patterns
| Wrong | Right |
|-------|-------|
| `field :amount, :float` | `field :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 loops | `Repo.preload(posts, :comments)` |
| `Repo.get!(User, user_id)` with user input | `Repo.get(User, id)` + handle nil |
| `{:ok, _} = Repo.update(cs)` inside `Repo.transaction` | `case`/`with` + `Repo.rollback(cs)` (see transactions.md) |
## References
For detailed patterns, see:
- `references/changesets.md` - cast vs put_change, custom validations, prepare_changes
- `references/queries.md` - Composable queries, dynamic, subqueries, preloading
- `references/migrations.md` - Safe migrations, concurrent indexes, NOT NULL
- `references/transactions.md` - Repo.transact, Ecto.Multi, upserts
Pliki w paczce (6)
- SKILL.md 3 KB
- references/changesets.md 4 KB
- references/fulltext-search.md 6 KB
- references/migrations.md 5 KB
- references/queries.md 5 KB
- references/transactions.md 4 KB
Wersje
| Repozytorium | Wynik | Licencja |
|---|---|---|
| Ta strona · oliver-kriska/claude-elixir-phoenix | 100 | MIT |
| oliver-kriska/claude-elixir-phoenix | 100 | MIT |
Identyczna treść występuje jeszcze w 1 innych folderach na GitHubie. Tu pokazujemy jedną, najlepiej ocenioną kopię.
Migawka bazy z 13.11.2026 · wersja paczki a91fd392e8af (skrót SHA-256 całej paczki)
Pobrania w ostatnich 30 dniach: 0 · ranking
Zgłoś problem z tym skillem: szkodliwa treść, błędny opis albo prośba autora o usunięcie.
Podobne skille
- 100ecto
Pomaga projektować i debugować warstwę danych w Elixirze z Ecto: schematy, changesety, zapytania, preloady, transakcje, migracje, multi-tenancy oraz wydajność i pulę połączeń. - 100ecto
Prowadzi przez schematy Ecto, changesety, zapytania, migracje i Ecto.Multi oraz pomaga wybrać między DSL zapytań Ecto a surowym SQL. - 100elixir-ecto-patterns
Opisuje wzorce Ecto w Elixirze: schematy, changesety, zapytania i transakcje, przy budowie aplikacji opartych na bazie danych. - 100estack-ecto
Implementuje dostęp do danych w Ecto: schematy, zapytania, changesety, transakcje i bezpieczne migracje, w tym konteksty ograniczone do tenanta. - 91ecto-changesets
Wspiera walidację i rzutowanie danych w changesetach Ecto: walidacje pól, ograniczenia, zagnieżdżone changesety i transformacje danych przed operacjami na bazie. - 100ecto-changeset-patterns
Pomaga pisać walidacje changesetów Ecto wykraczające poza cast i validate_required: walidacje międzypolowe, osobne changesety dla operacji, warunki, transformacje pól i unikalność.