Problem with update in Trigger

Asked

Viewed 45 times

0

I can not update the table due to Trigger the goal of it is to store the information of the changed record, so I wanted to know if the problem is in Foreign key(FK), if in that case I would have to create a NEW.id for all FK.

Code of the Trigger:

DROP TRIGGER  IF EXISTS trg_update_usuario;  
DELIMITER $$  
CREATE TRIGGER trg_update_usuario  
AFTER UPDATE ON usuario  
FOR EACH ROW   
BEGIN  
INSERT INTO auditoria_usuario(usuario_id_usuario,descricao,data_acao,usuario)
VALUES(NEW.id_usuario,'Alterado',NOW(),USER());  
END $$  
DELIMITER ;

After the

UPDATE `dpu`.`usuario` SET `nome` = 'Guilherme Roberto' 
WHERE (`id_usuario` = '1') 
and (`contato_id_contato` = '1') 
and (`cargo_id_cargo` = '1') 
and (`endereco_id_endereco` = '1');

returns the error:

ERROR 1452: 1452: Cannot add or update a child row: a foreign key
     constraint fails  (`dpu`.`auditoria_usuario`, CONSTRAINT
     `fk_auditoria_usuario_usuario1` FOREIGN KEY (`usuario_id_usuario`,
     `usuario_contato_id_contato`, `usuario_cargo_id_cargo`,
     `usuario_endereco_id_endereco`))

Descriptive image of the audit table. inserir a descrição da imagem aqui

  • please show the result of the command show create table dpu.auditoria_usuario so that we can analyze.

1 answer

0


The data you are entering in the table dpu.auditoria_usuario are insufficient as this table contains 5 primary keys (must be informed), of which 4 are foreign keys (the lack of them causes the error that is presented), and still has 3 more fields NOT NULL that is, you will only be able to enter records if you enter data for all table fields dpu.auditoria_usuario.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.