我有一个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
添加到我的默认更改集中,但错误仍然存在。
发布于 2016-11-09 21:59:37
Ecto有一个约束,用于检查如果没有与Ecto.Changeset.no_assoc_constraint/3
关联的记录,条目是否可以删除。通过这个验证,我得到了一个很好的错误,我可以像这样使用这个associated_records are still associated to this entry
。
另一个有用的约束是Ecto.Changeset.foreign_key_constraint/3
,它用于保证只有当父对象也存在于数据库中时才会创建子对象。
https://stackoverflow.com/questions/40498578
复制相似问题