“SQLSTATE[23000]: Integrity constraint violation”

2019-08-30 03:11发布

问题:

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

回答1:

Your fixture file is probably attempting to insert a row for County for which there is no corresponding row matching the accountId or the districtId.

ALTER TABLE County ADD CONSTRAINT FK_5F4EFA13438082DC FOREIGN KEY (insertedBy) REFERENCES Account(id);

This key forces you to ensure that the accountid you enter into the County table has a matching id in the account table.

ALTER TABLE County ADD CONSTRAINT FK_5F4EFA1362627EDC FOREIGN KEY (idDistrict) REFERENCES District(id);

Same as accountId, except no matching id in the district table.

So double check your fixtures file and ensure that that district and account rows are inserted (and committed) before you insert a county row.



回答2:

Actually there is a bug in MySQL server 5.6.33-79.0 Percona Server (GPL), Release 79.0, Revision 2084bdb in Linux (x86_64)



回答3:

Unistalled MAMP Pro and installed apache, mysql and php one by one. Same project worked under this new environment