ecto-nested-associations

Manage Ecto nested associations with cast_assoc, cast_embed, and Ecto.Multi.

149|15|Updated Jan 23, 2026
One-click install
npx skills add https://github.com/j-morgan6/elixir-phoenix-guide --skill ecto-nested-associations
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: ecto-nested-associations
Source: https://github.com/j-morgan6/elixir-phoenix-guide/tree/main/skills/ecto-nested-associations
Command: npx skills add https://github.com/j-morgan6/elixir-phoenix-guide --skill ecto-nested-associations

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This skill provides a rigorous, battle-tested set of patterns to manage nested associations in Ecto, preventing silent data inconsistencies when updating related records.

Core Features & Use Cases

  • Use cast_assoc for creating and updating has_many/has_one associations in a single operation
  • Use Ecto.Multi for multi-table transactions to ensure explicit rollback control
  • Use on_replace: :delete in cast_assoc to manage list-based associations safely
  • Preload associations before updates to avoid silent data loss during diffs
  • Use cast_embed for embedded schemas and ensure correct data shape
  • Follow explicit migrations and indexing guidelines to maintain data integrity

Quick Start

Apply these patterns to reliably manage nested associations in your Ecto schemas.

Frequently Asked Questions about ecto-nested-associations

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I use Ecto cast_assoc to update has_many associations in a single operation?

Ecto cast_assoc manages nested has_many associations by casting child changesets alongside the parent, ensuring safe creation and updates in one transaction. You must preload associations before updates to avoid silent data loss during list diffs.

When should I use Ecto.Multi instead of cast_assoc for nested associations?

Use Ecto.Multi for multi-table transactions when you need explicit rollback control across independent operations. Cast_assoc is better for parent-child updates, while Ecto.Multi chains discrete database operations with reliable atomic rollbacks.

Why does updating nested Ecto associations cause silent data loss without preloading?

Updating nested Ecto associations without preloading causes silent data loss because Ecto cannot diff existing records against incoming changes. Preloading ensures the current state is loaded so cast_assoc can accurately determine which records to update or delete.

How do I safely remove items from a has_many association using on_replace delete?

Configure on_replace: :delete in your schema to safely remove items from has_many associations during cast_assoc operations. This ensures Ecto deletes child records that are missing from the incoming changeset data, maintaining referential integrity.

Does Ecto cast_embed work with embedded schemas for managing nested data?

Yes, Ecto cast_embed works with embedded schemas to manage nested data entirely within a single table column. It ensures the correct data shape is validated and persisted without requiring separate database tables for the embedded records.

What migrations and indexing do I need for reliable Ecto nested association management?

Reliable Ecto nested association management requires explicit migrations and indexing to maintain data integrity. This includes adding foreign key constraints and database indexes to support safe propagation of changes and prevent orphaned records.