如何获取 doctrine 中 next table_id_seq (primary_key) 的值?

How to get the value for next table_id_seq (primary_key) in doctrine?

我在 symfony 4 中的一个项目中工作,数据库在 postgresql 中。有两种更新数据库的方法:

方法 1. 通过带有普通 sql 插入的 python 脚本

方法2.通过symfony形式

这两种方法写入相同的 table。

场景一:

方法 2. 使用 symfony 形式向 table 添加一行 (pid - 1)

方法1.使用python脚本添加500行(pid序列达到501)

方法 2。另一个用户尝试添加一行,而 doctrine 尝试使用 pid = 2 插入,但它应该是“502”

我尝试在实体定义中使用 GeneratedValue(strategy="AUTO") 和 "IDENTITY",但这并没有解决问题

/* 字段的实体 Class 定义 */

/**
  * @ORM\Id()
  * @ORM\GeneratedValue
  * @ORM\Column(type="integer")
  */
  private $id;

/* 表单提交处理器 */

$form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()){
            $data =$form->getData();
            $property = new Property();
            $property->setTitle($data['title']);
            $property->setDescription($data['description']);
            $property->setPrice($data['price']);

            $em->persist($property);
            $em->flush();

           return $this->redirectToRoute('property');
    }

可能的解决方案

如果存在,请提出最佳解决方案

查看您的实体 class 我认为为序列 table 中的 id 设置自动值的声明不完整。试试这个声明。它对我有用

/**
 * @ORM\Id()
 * @ORM\Column(type="integer", options={"default"="nextval('property_id_seq'::regclass)"})
 * @ORM\GeneratedValue(strategy="SEQUENCE")
 */
private $id;

删除现有 table 并从 bin/console

更新架构