为什么此代码失败 Ecto.NoPrimaryKeyValueError

Why does this code fail with Ecto.NoPrimaryKeyValueError

为什么会失败:

%Partner{} |> cast(%{id: 123}, [:id]) |> delete 

Ecto.NoPrimaryKeyValueError?我要明确设置主键吗?

对于变更集,原始结构 (data) 中的 idRepo.delete 使用,而不是 changes 中的 cast ] 将新的 id 仅放在 changes 中。您可以将 changes 合并到原始结构 (data):

%Partner{} |> cast(%{id: 123}, [:id]) |> Ecto.Changeset.apply_changes |> delete 

或手动将 id 放入 %Partner{}:

%Partner{id: 123} |> delete