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 - Use
Ecto.Multifor operations spanning multiple unrelated tables — do NOT useEcto.Multifor nested associations - Set
on_deleteexplicitly in migrations —:delete_allfor owned children,:nothingfor independent entities - Always create indexes on foreign key columns
- Use
on_replace: :deleteincast_assocfor list management — allows removing items by omitting them - Preload associations before updating them —
cast_assoccompares against currently loaded data - Do NOT require foreign keys in child changesets —
cast_assocsets them automatically - Use
Repo.transaction/1withEcto.Multi— wrap multi-table operations for atomicity
End-to-End Workflow
Follow this sequence for ANY nested association or multi-table operation: