Error de activación, MySQL

Well, I have researched and have not found a solution to the following problem 'SQL Error (1442): Can not update table' messages' in stored function / trigger because it is already used by statement Which invoked this stored function / trigger. `

My trigger is created, only when I run the INSERT on the table of messages, this error is thrown, my trigger is

DELIMITER $$ 
DROP TRIGGER IF EXISTS `onMessage` ;
CREATE TRIGGER `onMessage` BEFORE INSERT ON `messages` FOR EACH ROW 
BEGIN
       UPDATE `users` SET `users`.`messages` = ( `users`.`messages` + 1 ) 
       WHERE `users`.`uid` = NEW.uid ;
       DELETE FROM `messages` WHERE `date` < ( NOW( ) - INTERVAL 1 MINUTE ) ;
END ;
$$
DELIMITER ;

preguntado el 01 de febrero de 12 a las 14:02

3 Respuestas

This is a restriction in the use of triggers.

From the MySql documentaciones:

Dentro de una función o desencadenante almacenado, no se permite modificar una tabla que ya está siendo utilizada (para leer o escribir) por la declaración que invocó la función o desencadenador.

Respondido 01 Feb 12, 18:02

Correct, for MySQL anyway

You can't write to the table associated with a trigger in that trigger. It generates all manner of inconsistencies, not least because MySQL triggers are processed row-by-row (RBAR)

In this case, I'd use a stored procedure to deal with the INSERT on messages

Respondido 01 Feb 12, 18:02

It is not possible to update a table for which the trigger is created in the trigger since Mysql does some locking stuff on that table. Therefore you can't insert/update/delete rows of the same table because then the trigger would called again and again, ending up in a recursion.

If you want to create a trigger on the table which will update itself (perfoming a deletion in this case), make sure you use the NEW.column_name to refer to the row after it’s updated.

Respondido 01 Feb 12, 18:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.