I'm using Symfony 2 with Doctrine.
I have 4 classes: Country, District, County and Local. District has a foreign key of Country; County has a foreign of District; Local has a foreign key of District.
The problem is that when inserting a County (using data fixtures), I get the error
SQLSTATE[23000]: Integrity constraint violation:
I dumped the SQL to create the tables and constraints and got this:
CREATE TABLE Country (id INT AUTO_INCREMENT NOT NULL,
name VARCHAR(255) NOT NULL,
insertedAt DATETIME NOT NULL,
flag LONGTEXT DEFAULT NULL COMMENT '(DC2Type:object)',
PRIMARY KEY(id)) ENGINE = InnoDB;
CREATE TABLE County (id INT AUTO_INCREMENT NOT NULL,
name VARCHAR(255) NOT NULL,
insertedAt DATETIME NOT NULL,
insertedBy INT NOT NULL,
idDistrict INT NOT NULL,
INDEX IDX_5F4EFA13438082DC (insertedBy),
INDEX IDX_5F4EFA1362627EDC (idDistrict),
PRIMARY KEY(id)) ENGINE = InnoDB;
CREATE TABLE District (id INT AUTO_INCREMENT NOT NULL,
name VARCHAR(255) NOT NULL,
insertedAt DATETIME NOT NULL,
insertedBy INT NOT NULL,
idCountry INT NOT NULL,
INDEX IDX_C8B736D1438082DC (insertedBy),
INDEX IDX_C8B736D143CAA294 (idCountry),
PRIMARY KEY(id)) ENGINE = InnoDB;
CREATE TABLE LOCAL (id INT AUTO_INCREMENT NOT NULL,
name VARCHAR(255) NOT NULL,
insertedAt DATETIME NOT NULL,
insertedBy INT NOT NULL,
idCounty INT NOT NULL,
INDEX IDX_4A17A7EC438082DC (insertedBy),
INDEX IDX_4A17A7EC3BF357BF (idCounty),
PRIMARY KEY(id)) ENGINE = InnoDB;
ALTER TABLE County ADD CONSTRAINT FK_5F4EFA13438082DC
FOREIGN KEY (insertedBy) REFERENCES Account(id);
ALTER TABLE County ADD CONSTRAINT FK_5F4EFA1362627EDC
FOREIGN KEY (idDistrict) REFERENCES District(id);
ALTER TABLE District ADD CONSTRAINT FK_C8B736D1438082DC
FOREIGN KEY (insertedBy) REFERENCES Account(id);
ALTER TABLE District ADD CONSTRAINT FK_C8B736D143CAA294
FOREIGN KEY (idCountry) REFERENCES Country(id);
ALTER TABLE LOCAL ADD CONSTRAINT FK_4A17A7EC438082DC
FOREIGN KEY (insertedBy) REFERENCES Account(id);
ALTER TABLE LOCAL ADD CONSTRAINT FK_4A17A7EC3BF357BF
FOREIGN KEY (idCounty) REFERENCES County(id);
The problem is not in the DataFixture itself because I tried to insert a County using PhpMyAdmin and got the same error.
All tables are created in InnoDB engine and I can successfully create a Country and a District. The error occurs only with the County entity.
Thanks