title: ERROR 1452 : Cannot add or update a child row: a foreign key constraint fails
body: <p>Help me please! </p>
  
  <p>I am having this error.</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>I have these tables.</p>
  
  <p><img src="https://i.stack.imgur.com/f3Ij1.png" alt="enter image description here"></p>
  
  <pre><code>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
  ) 
  </code></pre>
  
  <p>When I do an insert into the table records the error pops up.</p>
  
  <pre><code>insert into registos values ('2014-03-31', '14:03:32', 'BrgTH032', '22.3', '45.3', '9.9', '32');
  </code></pre>
  
  <p>If I do this:</p>
  
  <pre><code>SET FOREIGN_KEY_CHECKS=0
  </code></pre>
  
  <p>the next insertion already accepted, but when I try again. back to give the same error.</p>
  
  <p>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.</p>
  
  <p>But I don't know how to resolve this.</p>
  
  <p>Help me please.</p>
  
  <p>-------EDIT( I used a trigger to populate the table Alarmes)------------------------</p>
  
  <pre><code>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)&lt;@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)&gt;@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  ;
  </code></pre>