Ejemplo
Manage unloaded association state with Ecto.Association.NotLoaded, handle put_assoc exceptions on uninitialized associations, and track retained parent state across build_assoc and apply_changes
sha256:273ee8a646360bca1986c52809ca47909c22e94a5026a62c46b23c56c2eefb3d
PUBLISHED
L3_CONTRACT_PASS
MIT-0
Evidencia de ejecución
Separamos el entorno declarado de las ejecuciones firmadas para mostrar qué se demostró.
Base de evidenciaContrato firmado aprobado
Recibos de verificación1
Nivel de verificaciónL3_CONTRACT_PASS
Entorno declarado
- Contexto de ejecución
- elixir
- Sistema operativo
- linux
- Arquitectura
- x64
- Runtime
- elixir
- Lenguaje
- elixir
- Gestor de paquetes
- mix
Entornos de las ejecuciones de verificación
- Contexto de ejecución
- elixir 1
- Sistema operativo
- linux alpine · musl
- Arquitectura
- x64
- Runtime
- elixir 1
- Lenguaje
- elixir
- Gestor de paquetes
- mix
- Ejecución
- container · docker
CONTAINER_RUN · compile:PASS · contract:PASS · load:PASS · resolve:PASS · hex@1 · 2026-08-17
Caso
- Objetivo
- Manage unloaded association state with Ecto.Association.NotLoaded, handle put_assoc exceptions on uninitialized associations, and track retained parent state across build_assoc and apply_changes HOW
- Paquetes
-
ecto 3.14.1
- Entorno
- elixir
- Creado
- 2026-08-17T04:43:50Z
Lo que suele suponerse
A competent developer expects put_assoc/4 to associate child records directly onto any struct or changeset, assuming schema association fields default to empty collections or nil when newly constructed.
El autor de la muestra anotó aquí lo que un desarrollador o un modelo esperaría. El contrato de abajo es lo que realmente se ejecutó.
Contrato
- Calling put_assoc/4 on a loaded struct with unloaded associations raises RuntimeError because uninitialized associations hold an %Ecto.Association.NotLoaded{} struct rather than nil or [], and Ecto.assoc_loaded?/1 returns false.
- Explicitly initializing the association field on the struct to [] (for has_many) or nil (for has_one/belongs_to) marks the association as loaded with Ecto.assoc_loaded?/1 returning true, allowing put_assoc/4 to succeed on loaded structs.
- Calling Ecto.build_assoc/3 constructs the child schema struct with the parent's primary key mapped to foreign keys, but leaves the parent struct's association field retained as %Ecto.Association.NotLoaded{}.
- Executing apply_changes/1 updates fields and embeds into structs while preserving %Ecto.Association.NotLoaded{} on untouched associations, and leaves prepare_changes/2 lifecycle callbacks unexecuted.
- Passing parameters with unpermitted fields to cast/4 populates changeset.changes with permitted fields only, but retains all unpermitted and sensitive keys in changeset.params.
Archivos
- NOTES.md
- csx.json
- mix.exs
- mix.lock
- test/contract.exs
- test/test_helper.exs
Descargar el artefacto de código fuente (tar.gz)
Seeder de origen
csx-seed
Recibos de verificación
- elixir 1 · linux alpine/x64 · docker · CONTAINER_RUN · compile:PASS · contract:PASS · load:PASS · resolve:PASS · hex@1 · 2026-08-17 · ed25519:d91480838ac982c9