php - 交易期间 key '...' 的重复条目 'PRIMARY'

标签 php mysql symfony doctrine-orm transactions

这件事情是昨晚发生在我身上的。我非常熟悉错误的性质,但我仍然无法弄清楚是什么原因造成的。我可能有预感,但我不确定。我将从一些基本的应用信息开始:​​

我的应用有 3 个实体:LoanSystemPageTextPage。每当有人添加贷款时,一个或多个系统页面就会被添加到数据库中。基本上,它是这样的:

if ( $form->isValid()){
    $this->em->getConnection()->beginTransation();
    $this->em->persist($loan);
    $this->em->flush();

    while ($someCondition){
        $page = new SystemPage();
        //... Fill the necessary data into page
        $page->setObject($loan);
        $this->em->persist($page);
    }

    $this->em->flush();
    $this->em->getConnection()->commit();
}

请忽略潜在的错别字,我是靠内存写的

实体 Loan 被映射到表 loans 并且 SystemPage 被映射(通过继承映射)到 system_pages 并且base_pages。后两者都有 id 字段设置为 AUTO_INCREMENT

我的直觉:还有另一个名为 text_pages 的表。考虑到一方面是 text_pagesbase_pages ,另一方面是 system_pagesbase_pages 共享 ID,我认为它很容易导致这种情况:

User1: Create BasePage, acquire autoincrement ID (value = 1)
User2: Create BasePage, acquire autoincrement ID (value = 1)
User1: Create TextPage, use the ID from step 1
User2: Create SystemPage, use the ID from step 2

这个理论的两个问题:

  • 交易。这就是我首先使用它们的原因
  • 在出错时没有其他用户在应用上进行其他事件

重要:稍等一分钟,重新提交就OK了。

这会不会是一些奇怪的 MySQL 事务隔离错误?任何提示将不胜感激...

编辑:

数据库模式的一部分:

enter image description here

请忽略塞尔维亚语列名

最佳答案

flush() 操作会在单个事务中刷新所有更改,因此这里有冗余代码...

您没有说明是否可以重现此错误,如果您可以提供数据库模式会很方便。

关于php - 交易期间 key '...' 的重复条目 'PRIMARY',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32364189/

相关文章:

php - DBpedia信息抽取框架

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

PHP:为什么这个加法的答案是 20 而不是 22?

fetch_array 中的 PHP MySQL 数组

php - 具有与参数中传递的值一样多的结果的 SQL 原则查询

javascript - 如何将 PHP smarty 模板与 Vue.js 集成?

mysql - 我正在从第一列的 CSV 文件中获取 MySQL 中导入的所有数据。所有其他列都获取 NULL 值

php - 构建 php 评估表

php - 如何使用来自非托管实体的数据正确更新托管实体?

symfony - Symfony/twig 中的分组复选框