简体   繁体   中英

“SQLSTATE[23000]: Integrity constraint violation” with valid constraint

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

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.

在MySQL(x86_64)中,MySQL服务器5.6.33-79.0 Percona Server(GPL),版本79.0,修订版2084bdb中实际上存在一个错误

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM