【问题标题】:Why does this code fail with Ecto.NoPrimaryKeyValueError为什么此代码因 Ecto.NoPrimaryKeyValueError 而失败
【发布时间】:2016-12-29 17:55:19
【问题描述】:

为什么会失败:

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

Ecto.NoPrimaryKeyValueError?我要显式设置主键?

【问题讨论】:

    标签: elixir ecto


    【解决方案1】:

    对于变更集,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
    

    【讨论】:

    • 不错的答案,新的长生不老药并解决了我的问题。这个答案向我解释了变更集实际上非常好。
    猜你喜欢
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 2012-09-08
    • 2013-09-24
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多