This is the error I receive
SQLSTATE [HY000]: General error: 1442 Can not update table 'accounts_cobrate' in stored function / trigger because it is used by statement which invoked this stored function / trigger.
If I use a static id instead of the query to get the ID of the account to be charged within INSERT , everything works normally.
Here are the 2 procedures:
TRIGGER tr_cuentascobrar_nota (In this trigger, the problem occurs):
CREATE TRIGGER tr_cuentascobrar_nota AFTER UPDATE
ON nc_factura FOR EACH ROW
BEGIN
IF ( NEW.estadoenvio = 1 ) THEN
INSERT INTO detalle_cuentas_cobrar ( cuenta_cobrar, monto_cancelado,fecha_actualizacion, banco )
VALUES ( (SELECT (idcuentas_cobrar) FROM cuentas_cobrar WHERE comprobante = NEW.comprobante_id), NEW.Total, NOW(), 20 );
END IF ;
END;
TRIGGER updateCount Account:
CREATE TRIGGER actualizarCuentaCobrar BEFORE INSERT
ON detalle_cuentas_cobrar FOR EACH ROW
BEGIN
UPDATE cuentas_cobrar SET monto_rest = monto_rest - NEW.monto_cancelado
WHERE idcuentas_cobrar = NEW.cuenta_cobrar;
IF ( (SELECT(monto_rest) FROM cuentas_cobrar WHERE idcuentas_cobrar = NEW.cuenta_cobrar ) = 0) THEN
UPDATE cuentas_cobrar SET estado = 'PAGADO' WHERE idcuentas_cobrar = NEW.cuenta_cobrar;
END IF ;
END