Hello, when I want to delete a
category I have just deleted the category always with
on_delete: :delete_all and because I used that all of my post were deleted, but this time I need to show an error massage to my client when they need to delete some
category they should delete all post of the category first and next can delete category.
now I change my reference in migration
add :category_id, references(:blog_categories, on_delete: :delete_all, type: :uuid)
add :category_id, references(:blog_categories, on_delete: :nothing, type: :uuid)
now I add it to my
|> foreign_key_constraint(:category_id, message: "this category has some post and you can't delete it")
but it doesn’t work and shows me an error:
** (exit) an exception was raised: ** (Ecto.ConstraintError) constraint error when attempting to delete struct: * blog_posts_category_id_fkey (foreign_key_constraint) If you would like to stop this constraint violation from raising an exception and instead add it as an error to your changeset, please call `foreign_key_constraint/3` on your changeset with the constraint `:name` as an option.
I just want to show a custom error I need how can do it ?