I need some help writing a MySQL trigger.
what i want to do is to calculated hashe value of autoincrement ID and store it in separate column:
create trigger hashing after insert on categories
for each row begin
set new.hashed = md5(CONCAT("key",`categories `.`id`));
end;
but i get error:
[Err] 1362 - Updating of NEW row is not allowed in after trigger
Can someone help me achieve what i need? Is this possible at all using triggers?
Thanks
What you are trying to do is not supported.
You can only set the value of the “hashed” column in a
BEFORE INSERTtrigger, not anAFTER INSERTtrigger.However, you can’t access the value of the auto-increment “id” column in a
BEFORE INSERTtrigger because that value is not known yet.If you are determined to do this via a trigger, one option is to create a separate table to store the hashed value (along with a foreign key to reference the “categories” table), and insert into that table using an
AFTER INSERTtrigger on your “categories” table.