MySQL triggers cannot update rows in same table the trigger is assigned to. Suggested workaround?

Posted by Cory House on Stack Overflow See other posts from Stack Overflow or by Cory House
Published on 2010-02-25T14:08:40Z Indexed on 2010/05/14 12:04 UTC
Read the original article Hit count: 271

MySQL doesn't currently support updating rows in the same table the trigger is assigned to since the call could become recursive. Does anyone have suggestions on a good workaround/alternative? Right now my plan is to call a stored procedure that performs the logic I really wanted in a trigger, but I'd love to hear how others have gotten around this limitation.

Edit: A little more background as requested. I have a table that stores product attribute assignments. When a new parent product record is inserted, I'd like the trigger to perform a corresponding insert in the same table for each child record. This denormalization is necessary for performance. MySQL doesn't support this and throws:

Can't update table 'mytable' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. A long discussion on the issue on the MySQL forums basically lead to: Use a stored proc, which is what I went with for now.

Thanks in advance!

© Stack Overflow or respective owner

Related posts about mysql

Related posts about triggers