尝试删除struct时出现Ecto约束错误

Rom*_*z C 2 database postgresql ecto phoenix-framework

我有一个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.
Run Code Online (Sandbox Code Playgroud)

我试图foreign_key_constraint/3在两个模型中添加到我的默认变更集,但错误仍在继续.

Rom*_*z C 6

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

另一个有用的约束是Ecto.Changeset.foreign_key_constraint/3,这对于保证仅在父数据库中也存在父项时才有用.