MYSQL - Error Code: 1451. Can not delete or update a parent row: a foreign key constraint fails

-1

I have to do a trigger, where to be excluded an employee will also be excluded its dependent. But I can not exclude. the code looks like this:

delimiter $

create trigger TRG_excluiDependente_AD
after delete 
on Funcionario
for each row
begin
    -- alter table Dependente drop foreign key ID_Func;
    delete from Dependente where Dependente.ID_Func = old.ID_Func;
end$    

delimiter ;

drop trigger TRG_excluiDependente_AD;
alter table Dependente DROP foreign key  fk_dep_func ;
delete from Funcionario where ID_Func = 4;

And I get the title error, and I've removed the FK-> Dependent

    
asked by anonymous 09.12.2018 / 13:36

1 answer

0

RESOLVI

delimiter $

create trigger TRG_excludes DependentBBS_BD - - > BD Before delete before delete on Official for each row begin

SET foreign_key_checks = 0;
delete from Dependente where Dependente.ID_Func = old.ID_Func;

end $

delimiter;

drop trigger TRG_excludes DependentBBS; delete from Functional where ID_Func = 1;

delimiter $

create trigger TRG_excludes Dependent_AD - - > AD After delete (after deleting) after delete on Official for each row begin

SET foreign_key_checks = 1;

end $

delimiter;

Using this function that temporarily "cancels" the foreign keys

    
10.12.2018 / 14:35