pabloqpacin
pabloqpacin

Reputation: 1

MariaDB 1442: create user FOO if not exists and update some FK to FOO's id

Honestly I think the code is pretty clear but I can't figure out how to deal with this 'recursion'. Any help would be appreciated!!

MariaDB [ventas]> DELIMITER //
               -> CREATE TRIGGER eliminarIdclientePedidos
               -> BEFORE DELETE ON cliente
               -> FOR EACH ROW
               -> BEGIN
               ->     -- Check if the user with id 1001 exists, create it if it doesn't
               ->     IF (SELECT COUNT(*) FROM cliente WHERE id = 1001) = 0 THEN
               ->         INSERT INTO cliente (id, nombre, apellido1) VALUES (1001, '(Eliminado)', '(Eliminado)');
               ->     END IF;
               ->
               ->     update pedido set id_cliente = 1001 where id = OLD.id;
               -> END //
               -> DELIMITER ;
Changed delimiter to //
Time: 0.001s

Query OK, 0 rows affected
Time: 0.005s

Changed delimiter to ;
Time: 0.000s

MariaDB [ventas]> delete from cliente where id in (3,4);
(1442, "Can't update table 'cliente' in stored function/trigger because it is already used by statement which invoked this stored function/trigger")

I would expect the user with id 1001 to be created.

I've seen quite a few posts here and elsewhere and it seems to be a tricky error message. Perhaps another approach would befit the usecase below better.


For extra-clarity, this is what I'm actually trying to solve (delete user):

MariaDB [ventas]> DELIMITER //
               -> CREATE TRIGGER auditarEliminacionClientesInactivos
               -> AFTER DELETE ON cliente
               -> FOR EACH ROW
               -> BEGIN
               ->     insert into auditoria (id_cliente, fecha_hora, mensaje)
               ->     values (OLD.id, NOW(),'Cliente eliminado por inactividad');
               -> END //
               -> DELIMITER ;
Changed delimiter to //
Time: 0.001s

Query OK, 0 rows affected
Time: 0.007s

Changed delimiter to ;
Time: 0.000s

MariaDB [ventas]> delete from cliente where id in (3,4);
(1451, 'Cannot delete or update a parent row: a foreign key constraint fails (`ventas`.`pedido`, CONSTRAINT `pedido_ibfk_1` FOREIGN KEY (`id_cliente`) REFERENCES `cliente` (`id`))')

UPDATE

This worked but still ain't great with thatSET FOREIGN_KEY CHECKS=0|1. Using it to circumvent the 1451 as above in a relatively safe way. Not in production anyways. If anything I'd create an old_orders table and delete the original entry rather than change the FK. Ah yes homework.

Thanks @ysth for your comment.

DELIMITER //
IF (SELECT COUNT(*) FROM cliente WHERE id = 42069) = 0 THEN
    INSERT INTO cliente (id, nombre, apellido1) VALUES (42069, '(Eliminado)', '(Eliminado)'); //
DELIMITER ;
DELIMITER //
CREATE TRIGGER nullifyIdclientePedidos
BEFORE DELETE ON cliente
FOR EACH ROW
BEGIN
    -- https://stackoverflow.com/questions/1905470/cannot-delete-or-update-a-parent-row-a-foreign-key-constraint-fails
    SET FOREIGN_KEY_CHECKS=0;
    update pedido set id_cliente = 42069 where id = OLD.id;
    SET FOREIGN_KEY_CHECKS=1;
END //
DELIMITER ;
delete from cliente where id in (3,4);
select * from auditoria where id_cliente in (3,4);

Upvotes: 0

Views: 45

Answers (0)

Related Questions