MySQL Forums
Forum List  »  Triggers

pre_insert trigger not working
Posted by: Todd Gould
Date: June 06, 2019 01:03PM

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!

Options: ReplyQuote


Subject
Views
Written By
Posted
pre_insert trigger not working
495
June 06, 2019 01:03PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.