Hey i have a trigger that fires after update that’s defined as following:
CREATE DEFINER = `root`@`localhost` TRIGGER `logs_m` AFTER UPDATE ON
`logs_month` FOR EACH ROW
BEGIN IF NEW.status =1 THEN INSERT INTO logs_payments( direction,
TYPE , amount, agent_id, invoice_number )
VALUES ( 0, 3, NEW.reward_minutes, NEW.agent_id, NULL ) ,
( 0, 5, NEW.credit, NEW.agent_id, NULL ) ;
END IF ;
END
Now on logs_payments i have a trigger on after insert which is defined like this :
CREATE DEFINER = `root`@`localhost` TRIGGER `payments_invoice` BEFORE INSERT ON
`logs_payments`
FOR EACH
ROW BEGIN
IF NEW.direction =0
THEN
SET NEW.invoice_number = ( SELECT MAX( IFNULL( invoice_number, 100000000 ) ) +1
FROM logs_payments l
WHERE l.direction =0 ) ;
END IF ;
IF NEW.direction =1 THEN SET NEW.invoice_number =
( SELECT MAX( IFNULL( invoice_number, 200000000 ) ) +1
FROM logs_payments l
WHERE l.direction =1 ) ;
END IF ;
END
Now the the first inserted row invoice_number gets null and the 2nd gets the right value.
Any idea why that should happen?
Solved it, the triggers where fine I had a problem in my query, since I didn’t have value on first I set :
Which was wrong it should be
And now it works fine.