pre_insert trigger not working
I have the follow pre insert trigger on a table:
CREATE DEFINER=`maadmin`@`%` TRIGGER `ma`.`transaction_BEFORE_INSERT` BEFORE INSERT ON `transaction` FOR EACH ROW
BEGIN
IF (NEW.id IS NULL)
THEN
SET NEW.id = ma.uuid_to_bin(UUID());
END IF;
END
The uuid_to_bin function is as follows:
CREATE DEFINER=`maadmin`@`%` FUNCTION `uuid_to_bin`(_uuid BINARY(36)) RETURNS binary(16)
DETERMINISTIC
SQL SECURITY INVOKER
RETURN
IF (_uuid IS NULL, NULL,
UNHEX(CONCAT(
SUBSTR(_uuid, 15, 4),
SUBSTR(_uuid, 10, 4),
SUBSTR(_uuid, 1, 8),
SUBSTR(_uuid, 20, 4),
SUBSTR(_uuid, 25) )))
Unfortunately, whenever I do an insert that does not specify the id column, it is getting set to \x00 (not the desired convert new UUID()). Any second insert will therefore violate the unique index I have in this column.
What am I do wrong please?
Thanks in advance!