E
E
entermix2018-03-06 19:16:30
MySQL
entermix, 2018-03-06 19:16:30

Why doesn't the MYSQL trigger fire?

There are the following tables:

users
id, discount, ...
coupons
id, code, ...
discounts
id, user_id, discount, ...

The following restrictions are configured:
ALTER TABLE `discounts`
  ADD CONSTRAINT `discounts_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE;

When deleting records from the discounts table, the trigger fires:
CREATE TRIGGER `after_delete_discounts` AFTER DELETE ON `discounts`
 FOR EACH ROW BEGIN

  UPDATE `users` SET `discount` = (SELECT SUM(`discount`) FROM `discounts` WHERE `user_id` = `OLD`.`user_id`) WHERE `id` = `OLD`.`user_id` LIMIT 1;
  
END

If I delete a record from the discounts table, the trigger fires
. If I delete a record from the coupons table, the records in the discounts table are deleted, but the trigger does not fire.
Why is that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-03-06
@entermix

From documentation :

Triggers are not activated by foreign key actions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question