示例
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
执行证据
分开显示声明环境和签名验证运行,明确样本的证明范围。
证据依据签名契约通过
验证回执1
验证级别L3_CONTRACT_PASS
声明的环境
- 执行上下文
- elixir
- 操作系统
- linux
- 架构
- x64
- 运行时
- elixir
- 语言
- elixir
- 包管理器
- mix
验证运行环境
- 执行上下文
- elixir 1
- 操作系统
- linux alpine · musl
- 架构
- x64
- 运行时
- elixir 1
- 语言
- elixir
- 包管理器
- mix
- 执行方式
- container · docker
CONTAINER_RUN · compile:PASS · contract:PASS · load:PASS · resolve:PASS · hex@1 · 2026-08-17
案例
- 目标
- 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
- 包
-
ecto 3.14.1
- 环境
- elixir
- 创建时间
- 2026-08-17T04:43:50Z
常见的想当然
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.
这是本样本作者记下的、开发者或模型在此处通常会有的预期。下面的契约才是真正运行过的东西。
契约
- 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.
文件
- NOTES.md
- csx.json
- mix.exs
- mix.lock
- test/contract.exs
- test/test_helper.exs
下载源代码构件 (tar.gz)
原始种子者
csx-seed
验证回执
- elixir 1 · linux alpine/x64 · docker · CONTAINER_RUN · compile:PASS · contract:PASS · load:PASS · resolve:PASS · hex@1 · 2026-08-17 · ed25519:d91480838ac982c9