【发布时间】:2016-12-29 17:55:19
【问题描述】:
为什么会失败:
%Partner{} |> cast(%{id: 123}, [:id]) |> delete
Ecto.NoPrimaryKeyValueError?我要显式设置主键?
【问题讨论】:
为什么会失败:
%Partner{} |> cast(%{id: 123}, [:id]) |> delete
Ecto.NoPrimaryKeyValueError?我要显式设置主键?
【问题讨论】:
对于变更集,Repo.delete 使用原始结构 (data) 中的 id,而不是 changes 中的那个,cast 仅将新的 id 放入 changes .您可以将changes 合并到原始结构(data)中:
%Partner{} |> cast(%{id: 123}, [:id]) |> Ecto.Changeset.apply_changes |> delete
或手动将id 放入%Partner{}:
%Partner{id: 123} |> delete
【讨论】: