I'm currently breaking my head on a 1452 error (using Symfony2 with doctrine).
Here is the case: I have 3 tables, Location, Concert and CD, and as expected a Concert take place at a Location, and a CD is related to a Concert, that's including some foreign keys.
Here is the SQL queries generated by doctrine to create the database:
CREATE TABLE CD (id INT AUTO_INCREMENT NOT NULL, concert INT NOT NULL, number INT NOT NULL, INDEX IDX_EB3C8BB0D57C02D2 (concert), PRIMARY KEY(id)) ENGINE = InnoDB;
CREATE TABLE Concert (id INT AUTO_INCREMENT NOT NULL, location INT NOT NULL, date DATETIME NOT NULL, name VARCHAR(255) DEFAULT NULL, INDEX IDX_1AC13B4E5E9E89CB (location), PRIMARY KEY(id)) ENGINE = InnoDB;
CREATE TABLE Location (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(40) NOT NULL, city VARCHAR(40) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB;
ALTER TABLE CD ADD CONSTRAINT FK_EB3C8BB0D57C02D2 FOREIGN KEY (concert) REFERENCES Concert(id) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE Concert ADD CONSTRAINT FK_1AC13B4E5E9E89CB FOREIGN KEY (location) REFERENCES Location(id) ON UPDATE CASCADE ON DELETE CASCADE;
The fact is, there is no problem to insert Location, and a connected Concert, to satisfy the CD's foreign key:
INSERT INTO Location (id, name, city) VALUES (NULL, 'Church', 'Berlin');
INSERT INTO Concert (id, location, date, name) VALUES (NULL, '1', '2012-06-20 19:30:00', NULL);
But then if I try to insert a CD:
INSERT INTO CD (id ,concert ,number) VALUES (NULL , '1', '1');
I got the famous error:
Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`festival`.`cd`, CONSTRAINT `FK_EB3C8BB0D57C02D2` FOREIGN KEY (`concert`) REFERENCES `Concert` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
So I've tried some hacks:
Escape the creation of the table Location and the related key:
CREATE TABLE CD (id INT AUTO_INCREMENT NOT NULL, concert INT NOT NULL, number INT NOT NULL, INDEX IDX_EB3C8BB0D57C02D2 (concert), PRIMARY KEY(id)) ENGINE = InnoDB; CREATE TABLE Concert (id INT AUTO_INCREMENT NOT NULL, location INT NOT NULL, date DATETIME NOT NULL, name VARCHAR(255) DEFAULT NULL, INDEX IDX_1AC13B4E5E9E89CB (location), PRIMARY KEY(id)) ENGINE = InnoDB; ALTER TABLE CD ADD CONSTRAINT FK_EB3C8BB0D57C02D2 FOREIGN KEY (concert) REFERENCES Concert(id) ON UPDATE CASCADE ON DELETE CASCADE;
That's working well.
Delete the table Location after creation (and the related key):
CREATE TABLE CD (id INT AUTO_INCREMENT NOT NULL, concert INT NOT NULL, number INT NOT NULL, INDEX IDX_EB3C8BB0D57C02D2 (concert), PRIMARY KEY(id)) ENGINE = InnoDB; CREATE TABLE Concert (id INT AUTO_INCREMENT NOT NULL, location INT NOT NULL, date DATETIME NOT NULL, name VARCHAR(255) DEFAULT NULL, INDEX IDX_1AC13B4E5E9E89CB (location), PRIMARY KEY(id)) ENGINE = InnoDB; CREATE TABLE Location (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(40) NOT NULL, city VARCHAR(40) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB; ALTER TABLE CD ADD CONSTRAINT FK_EB3C8BB0D57C02D2 FOREIGN KEY (concert) REFERENCES Concert(id) ON UPDATE CASCADE ON DELETE CASCADE; ALTER TABLE Concert ADD CONSTRAINT FK_1AC13B4E5E9E89CB FOREIGN KEY (location) REFERENCES Location(id) ON UPDATE CASCADE ON DELETE CASCADE; ALTER TABLE Concert DROP FOREIGN KEY FK_1AC13B4E5E9E89CB; ALTER TABLE Concert DROP INDEX IDX_1AC13B4E5E9E89CB; DROP TABLE Location;
Same error.
Escape foreign key controlling
SET FOREIGN_KEY_CHECKS = 0;
Same error.
The two last tries, make me think of some index issue on the link Concert-Location ? You understand, I am a bit lost.
Does anyone have already faced something similar ?
Thank a lot for knowledge,
Tom.