ERROR 1452 : Cannot add or update a child row: a foreign key constraint fails

rpires_098 picture rpires_098 · Mar 31, 2014 · Viewed 11.9k times · Source

Help me please!

I am having this error.

Error: Cannot add or update a child row: a foreign key constraint fails (world.alarmes, CONSTRAINT fk_alarmes_registos1 FOREIGN KEY (idRegisto) REFERENCES registos (idRegisto) ON DELETE NO ACTION ON UPDATE NO ACTION)

I have these tables.

enter image description here

CREATE TABLE `registos` (
  `data_registo` char(10) NOT NULL,
  `hora_registo` time NOT NULL,
  `idSensor` varchar(8) NOT NULL,
  `Temperatura` char(6) DEFAULT NULL,
  `Humidade` char(6) DEFAULT NULL,
  `pt_orvalho` char(6) DEFAULT NULL,
  `idRegisto` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`idRegisto`,`idSensor`,`data_registo`,`hora_registo`),
  KEY `fk_registos_sensores1_idx` (`idSensor`),
  CONSTRAINT `fk_registos_sensores1` FOREIGN KEY (`idSensor`) REFERENCES `sensores` (`idSensor`) ON DELETE NO ACTION ON UPDATE NO ACTION
) 




CREATE TABLE `alarmes` (
  `idAlarme` int(11) NOT NULL AUTO_INCREMENT,
  `descricao_alarme` varchar(45) DEFAULT NULL,
  `data_criacao` datetime DEFAULT CURRENT_TIMESTAMP,
  `idRegisto` int(11) NOT NULL DEFAULT ''0'',
  PRIMARY KEY (`idAlarme`,`idRegisto`),
  KEY `fk_alarmes_registos1_idx` (`idRegisto`),
  CONSTRAINT `fk_alarmes_registos1` FOREIGN KEY (`idRegisto`) REFERENCES `registos` (`idRegisto`) ON DELETE NO ACTION ON UPDATE NO ACTION
) 

When I do an insert into the table records the error pops up.

insert into registos values ('2014-03-31', '14:03:32', 'BrgTH032', '22.3', '45.3', '9.9', '32');

If I do this:

SET FOREIGN_KEY_CHECKS=0

the next insertion already accepted, but when I try again. back to give the same error.

I've been researching and fails because the registos table references a foreign key from the sensores table. You can't directly insert into a relational table without there being a corresponding entry in the table that is being referenced.

But I don't know how to resolve this.

Help me please.

-------EDIT( I used a trigger to populate the table Alarmes)------------------------

DELIMITER $$
create TRIGGER alerta
BEFORE INSERT ON registos
FOR EACH ROW
begin
Set @tempmax=0;
Set @tempmin=0;


select lim_inf_temp, lim_sup_temp into @tempmin, @tempmax from sensores  where idSensor=NEW.idSensor;


Set @maxidAlarme=0;
if (CAST(NEW.Temperatura AS UNSIGNED)<@tempmin) then
SELECT MAX(idAlarme) into @maxidAlarme FROM alarmes;
SET @maxidAlarme=@maxidAlarme+1;
INSERT INTO alarmes(idAlarme,descricao_alarme, idRegisto) VALUES (@maxidAlarme,"temperatura inserida inferior ao normal",New.idRegisto);
end if; 


if (CAST(NEW.Temperatura AS UNSIGNED)>@tempmax) then
SELECT MAX(idAlarme) into @maxidAlarme FROM alarmes;
SET @maxidAlarme=@maxidAlarme+1;
INSERT INTO alarmes(idAlarme,descricao_alarme, idRegisto) VALUES (@maxidAlarme,"temperatura inserida superior ao normal",New.idRegisto);
end if; 

end $$;

DELIMITER  ;

Answer

Ryan Vincent picture Ryan Vincent · Mar 31, 2014

You are trying to insert more values into the table than allowed 7 (seven) but 6 (six) expected.

Please, always include the columns that you are inserting to in an 'insert' query.

There are seven columns in this table but one is an 'auto increment' column so there should be 6 (six) values in the insert query.

CREATE TABLE `registos` (
  `data_registo` char(10) NOT NULL,
  `hora_registo` time NOT NULL,
  `idSensor` varchar(8) NOT NULL,
  `Temperatura` char(6) DEFAULT NULL,
  `Humidade` char(6) DEFAULT NULL,
  `pt_orvalho` char(6) DEFAULT NULL,
  `idRegisto` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`idRegisto`,`idSensor`,`data_registo`,`hora_registo`),
  KEY `fk_registos_sensores1_idx` (`idSensor`),
  CONSTRAINT `fk_registos_sensores1

Here is the 'insert' query:

insert into registos values ('2014-03-31', '14:03:32', 'BrgTH032', '22.3', '45.3', '9.9', '32'); 

There are seven values but you would expect the query to look like (columns added):

insert into registos (data_registo, hora_registo, idSensor, Temperatura, Humidade, pt_orvalho)
values ('2014-03-31', '14:03:32', 'BrgTH032', '22.3', '45.3', '9.9', '32');

I suggest that the query should be:

insert into registos (data_registo, hora_registo, idSensor, Temperatura, Humidade, pt_orvalho)
values ('2014-03-31', '14:03:32', 'BrgTH032', '22.3', '45.3', '9.9');

The trigger on 'registos' must be an after insert trigger to pick up the NEW 'idRegisto' value.

create TRIGGER alerta
AFTER INSERT ON registos
FOR EACH ROW
begin