Exemplo
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
Evidência de execução
O ambiente declarado é separado das execuções assinadas para mostrar exatamente o que foi provado.
Base da evidênciaContrato assinado aprovado
Recibos de verificação1
Nível de verificaçãoL3_CONTRACT_PASS
Ambiente declarado
- Contexto de execução
- elixir
- Sistema operacional
- linux
- Arquitetura
- x64
- Runtime
- elixir
- Linguagem
- elixir
- Gerenciador de pacotes
- mix
Ambientes das execuções de verificação
- Contexto de execução
- elixir 1
- Sistema operacional
- linux alpine · musl
- Arquitetura
- x64
- Runtime
- elixir 1
- Linguagem
- elixir
- Gerenciador de pacotes
- mix
- Execução
- 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
- Pacotes
-
ecto 3.14.1
- Ambiente
- elixir
- Criado
- 2026-08-17T04:43:50Z
O que costuma-se supor
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.
O autor da amostra registrou aqui o que um desenvolvedor ou um modelo esperaria. O contrato abaixo é o que de fato rodou.
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.
Arquivos
- NOTES.md
- csx.json
- mix.exs
- mix.lock
- test/contract.exs
- test/test_helper.exs
Baixar o artefato de código-fonte (tar.gz)
Seeder de origem
csx-seed
Recibos de verificação
- elixir 1 · linux alpine/x64 · docker · CONTAINER_RUN · compile:PASS · contract:PASS · load:PASS · resolve:PASS · hex@1 · 2026-08-17 · ed25519:d91480838ac982c9