简体   繁体   English

外键约束失败,因为insert语句尚未提交(MySQL)

[英]Foreign key constraint fails because insert statement not yet committed (MySQL)

I have a PHP script using Doctrine 2 which does essentially the following: 我有一个使用Doctrine 2的PHP脚本,它主要执行以下操作:

$entityManager->transactional(function($em) {
    $foreignObject = new DoctrineEntities\ForeignTable();
    $em->persist($foreignObject);
    $em->flush();
    $aObject = new DoctrineEntities\A();
    $aObject->ForeignID = $foreignObject->ID;
    $em->persist($aObject);
    $em->flush();
});

I'm getting an integrity constraint violation: 我收到了完整性约束违规:

a foreign key constraint fails (dbName.A, CONSTRAINT A_ForeignID FOREIGN KEY (ForeignID) REFERENCES ForeignTable ( ID ) ON DELETE NO ACTION ON UPDATE NO ACTION) 外键约束失败(dbName.A,CONSTRAINT A_ForeignID FOREIGN KEY( ForeignTable )REFERENCES ForeignTableID )ON DELETE NO ACTION ON UPDATE NO ACTION)

My guess is that the constraint is checked before the commit, and it doesn't check to see whether an insert I made that hasn't been committed yet might make the constraint pass rather than fail. 我的猜测是在提交之前检查约束,并且它不会检查我所做的尚未提交的插入是否可以使约束通过而不是失败。 But I really do want these two insert statements wrapped in the same transaction. 但我确实希望这两个插入语句包含在同一个事务中。 So what can I do? 那我该怎么办?

UPDATE UPDATE

I moved $em->persist($aObject); $em->flush(); 我移动$em->persist($aObject); $em->flush(); $em->persist($aObject); $em->flush(); out of the transaction and I'm still getting the same error. 退出交易,我仍然得到同样的错误。 Apparently, my guess was wrong... But then I really don't know what's causing the error. 显然,我的猜测是错误的...但后来我真的不知道是什么导致错误。


SQL Context SQL上下文

Table A A.

CREATE TABLE `A` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `ForeignID` int(11) NOT NULL,
  PRIMARY KEY (`ID`),
  KEY `A_ForeignID` (`ForeignID`),
  CONSTRAINT `A_ForeignID` FOREIGN KEY (`ForeignID`) REFERENCES `ForeignTable` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci

Table ForeignTable ForeignTable

CREATE TABLE `ForeignTable` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci

I'd suggest to read about MySQL data integrity and FKs , then Doctrine associations , data integrity is checked by MySQL for InnodDB tables. 我建议阅读有关MySQL数据完整性和FK ,然后是Doctrine关联 ,MySQL会检查InnodDB表的数据完整性。 What you're doing is not right, it should be 你做的不对,它应该是

$entityManager->transactional(function($em) {
    $foreignObject = new DoctrineEntities\ForeignTable();
    $em->persist($foreignObject);

    $aObject = new DoctrineEntities\A();
    $aObject->setForeign($foreignObject);
    $em->persist($aObject);

    $em->flush();
});

I solved the problem. 我解决了这个问题。 It was somewhere else completly unrelated to my question.. I'm going to accept getme's answer instead because accepting this answer really won't help anyone else here... 这是与我的问题完全无关的其他地方..我会接受getme的答案,因为接受这个答案真的不会帮助其他任何人......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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