This is factors table:

CREATE TABLE `factors` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `price_sum` int(11) NOT NULL,
  `date` datetime DEFAULT NULL,
  PRIMARY KEY (`id`))

And this one is subfactors table:

CREATE TABLE `subfactors` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `price` int(11) NOT NULL,
  `factor_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY subfactors(`factor_id`) REFERENCES factors(`id`))

I've tried this query but it says that I can't insert a row into a child table:

DELIMITER //
CREATE TRIGGER my_trigger AFTER INSERT
ON subfactors FOR EACH ROW
BEGIN
    IF NEW.factor_id=NULL THEN 
        INSERT INTO factor(id,price_sum) 
        VALUES(NEW.factor_id,NEW.price);
    END IF;
END //

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails

0

There are 0 best solutions below