I have a table where I auto-generate a binary(16) Id by having this in my defaultExpression:
(uuid_to_bin(uuid(), true))
I created a store procedure and insert values to that table and figured I could retrive the latest value by using LAST_INSERT_ID() but Im getting the value 0 instead.
Example:
DECLARE userId BINARY(16);
INSERT INTO Users (Email, Password, FirstName, LastName, Age, Jobtitle, ImageId, PrefLang, ChangedPassword)
VALUES ('email', 'Hashpassword', 'firstName', 'lastName', 'age', 'jobTitle', 'imageId', 'prefLanguagId', 0);
SET userId = LAST_INSERT_ID(); // not getting the correct value here
Trying to use the value above in a junction table:
INSERT INTO UserRoles (UserId, RoleId)
VALUES (userId, roleId);
Error Code: 1452 Cannot add or update a child row: a foreign key constraint fails (`dbName`.`userroles`, CONSTRAINT
UserIdRefFOREIGN KEY (`UserId`) REFERENCES `users` (`Id`))
How can I solve this?
LAST_INSERT_ID()when using an auto increment column.