Cannot delete or update parent row; foreign key constraint does not remove rails

When I tried to delete a value in rails, I received the following error: " cannot delete or update the parent row, foreign key constraint will cause the rails to fail "

This is perfectly acceptable for me, since I do not want the user to be able to delete the value if it is associated with another value. However, what is the best way to catch the exception and then notify the user that this value cannot be deleted?

Thank,

TW

+4
source share
2 answers

. , , before_destroy . rescue_from .

0
*********** Mysql Error:
"Cannot delete or update a parent row: a foreign key constraint fails."

(mysql), ( ) , ActiveRecord::RecordNotUnique

, catch:

begin
   <delete action expected to raise the above mentioned error>
rescue Exception => e
  logger.debug "#{e.class}"
end
0

Source: https://habr.com/ru/post/1570020/


All Articles