ecto-nested-associations
Installation
SKILL.md
Ecto Nested Associations
RULES — Follow these with no exceptions
- Use
cast_assoc/3for has_many/has_one — never manually insert children in a separate step; let Ecto manage the relationship - Use
Ecto.Multifor operations spanning multiple unrelated tables — not nested changesets; Multi provides explicit rollback control - Set
on_deleteexplicitly in migrations —:delete_allfor owned children,:nothingfor references to independent entities - Always create indexes on foreign key columns — missing FK indexes cause slow joins and lookups on the child table
- Use
on_replace: :deleteincast_assocfor list management — allows removing items by omitting them from the input - Preload associations before updating them —
cast_assoccompares against currently loaded data; unloaded associations cause silent data loss
cast_assoc for Nested Creates
Create parent and children in a single operation. Ecto sets foreign keys automatically.
# Schema definitions
Related skills
More from j-morgan6/elixir-phoenix-guide
oban-essentials
MANDATORY for ALL Oban work. Invoke before writing workers or enqueuing jobs.
1phoenix-json-api
MANDATORY for ALL JSON API work. Invoke before writing API controllers, pipelines, or JSON responses.
1ecto-essentials
MANDATORY for ALL database work. Invoke before modifying schemas, queries, or migrations.
1otp-essentials
MANDATORY for ALL OTP work. Invoke before writing GenServer, Supervisor, Task, or Agent modules.
1code-quality
Automated code quality detection — duplication, complexity, unused functions. Invoke when analyzing or refactoring Elixir code.
1phoenix-uploads
MANDATORY for file upload features. Invoke before implementing upload or file serving functionality.
1