ecto-nested-associations

Installation
SKILL.md

Ecto Nested Associations

RULES — Follow these with no exceptions

  1. Use cast_assoc/3 for has_many/has_one — never manually insert children in a separate step; let Ecto manage the relationship
  2. Use Ecto.Multi for operations spanning multiple unrelated tables — not nested changesets; Multi provides explicit rollback control
  3. Set on_delete explicitly in migrations:delete_all for owned children, :nothing for references to independent entities
  4. Always create indexes on foreign key columns — missing FK indexes cause slow joins and lookups on the child table
  5. Use on_replace: :delete in cast_assoc for list management — allows removing items by omitting them from the input
  6. Preload associations before updating themcast_assoc compares 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
Installs
1
GitHub Stars
118
First Seen
Apr 21, 2026