Napisałem sobie trigger, który ma aktualizować kolumnę "position" po usunięciu danego wpisu z tej własnie tabeli. Tak, aby po prostu numeracja była zachowana po usunięciu.
CREATE TRIGGER language_trigger_after_delete after DELETE ON LANGUAGE FOR each row begin declare current_position integer; declare updateid integer; declare update_position cursor FOR SELECT id FROM LANGUAGE WHERE position > old.position ORDER BY position ASC; SET current_position = (SELECT position FROM LANGUAGE WHERE id = old.id); open update_position; upd_loop: LOOP fetch update_position INTO updateid; UPDATE `language` SET position = (position - 1) WHERE id = updateid; end LOOP; close update_position; end;
Otrzymuje ostatecznie komunikat:
Kod
#1442 - Can't update table 'language' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Komunikat jest dla mnie w miarę jasny, jednak rozwiązanie problemu nie przychodzi mi do głowy.