【问题标题】:Ecto constraint error when attempting to delete struct尝试删除结构时出现 Ecto 约束错误
【发布时间】:2016-11-09 00:18:34
【问题描述】:

我有一个User 模型has_many 医疗信息和医疗信息belongs_to 用户。

当我尝试删除具有关联记录(医疗信息)的User 时,Ecto 给我以下错误:

 ** (Ecto.ConstraintError) constraint error when attempting to delete struct:

     * foreign_key: medical_informations_user_id_fkey

 If you would like to convert this constraint into an error, please
 call foreign_key_constraint/3 in your changeset and define the proper
 constraint name. The changeset has not defined any constraint.

我尝试将foreign_key_constraint/3 添加到两个模型的默认变更集中,但错误仍然存​​在。

【问题讨论】:

    标签: database postgresql phoenix-framework ecto


    【解决方案1】:

    如果没有关联的记录Ecto.Changeset.no_assoc_constraint/3,Ecto 有一个约束来检查是否可以删除条目。通过这个验证,我得到了一个很好的错误,我可以像这样使用associated_records are still associated to this entry

    另一个有用的约束是Ecto.Changeset.foreign_key_constraint/3,它有助于保证只有当父级也存在于数据库中时才会创建子级。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-08
    • 2014-12-02
    • 2015-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-07
    相关资源
    最近更新 更多