I have a User model that has_many Medical Informations, and Medical Informations belongs_to User.
When i try to delete a User that has associated records (medical informations), Ecto gives me the following error:
** (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.
I tried to add foreign_key_constraint/3 to my default changeset in both models but the error continues.
Ecto has a constraint to check if an entry can be deleted if doesn't have records associated Ecto.Changeset.no_assoc_constraint/3. With this validation i get a nice error that i can use like this associated_records are still associated to this entry.
Another useful constraint is Ecto.Changeset.foreign_key_constraint/3, that is useful to guarantee that a child will only be created if the parent exists in the database too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With