【问题标题】:Symfony / Doctrine - Id already existSymfony / Doctrine - Id 已经存在
【发布时间】:2017-07-21 17:11:21
【问题描述】:

我有问题。我将网站移动到另一个主机(所以我导出数据库并导入到新主机)。 但是现在,当我尝试将新记录添加到数据库时。我有一个错误 stn,例如 ID 1 已存在。但我的表中有近 500 条记录。

我能做些什么来解决它?

抱歉,我应该提供更多信息:

我通过 phppgadmin 导出和导入数据库我没有使用迁移包。

这是我的实体:

class Products
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="serial_number", type="string", length=255, nullable=true)
     */
    private $serial;

    /**
     * @var string
     * @Expose
     * @ORM\Column(name="title", type="string", length=255)
     */
    private $title;

    /**
     * @var string
     * @Expose
     * @ORM\Column(name="url", type="string", length=255)
     */
    private $url;

    /**
     * @var string
     * @ORM\Column(name="note", type="text", nullable=true)
     */
    private $note;

    /**
     * @var int
     *
     * @ORM\Column(name="views", type="bigint", nullable=true)
     */
    private $views;

    /**
     * @ORM\ManyToMany(targetEntity="Models")
     * @ORM\JoinTable(name="products_models",
     *      joinColumns={@ORM\JoinColumn(name="product_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="model_id", referencedColumnName="id")}
     *      )
     */
    private $models;

    /**
     * @ORM\OneToOne(targetEntity="ProductDetails", cascade={"persist", "remove"})
     * @ORM\JoinColumn(name="details_id", referencedColumnName="id")
     */
    private $details;

    /**
     * @var File
     * @Expose
     * @ORM\OneToMany(targetEntity="ProductImages", mappedBy="product", cascade={"persist", "remove"})
     * @ORM\OrderBy({"id" = "ASC"})
     *
     */
    private $images;

    /**
     * @var File
     *
     * @ORM\OneToMany(targetEntity="Cart", mappedBy="productId", cascade={"persist"})
     *
     */
    private $cart;


    /**
     * @var string
     *
     * @ORM\Column(name="price", type="integer", length=255, nullable=true)
     */
    private $price;

    /**
     * @var string
     *
     * @ORM\Column(name="bought_price", type="integer", length=255, nullable=true)
     */
    private $boughtPrice;

    /**
     * @var string
     *
     * @ORM\Column(name="old_price", type="integer", length=255, nullable=true)
     */
    private $oldPrice;


    /**
     * @var bool
     *
     * @ORM\Column(name="is_active", type="boolean", nullable=true)
     */
    private $isActive;

    /**
     * @var bool
     *
     * @ORM\Column(name="is_accessory", type="boolean", nullable=true)
     */
    private $isAccessory;

    /**
     * @ORM\ManyToOne(targetEntity="AccessoryCategory")
     * @ORM\JoinColumn(name="accessory_category_id", referencedColumnName="id")
     */
    private $accessoryCategory;

    /**
     * @var bool
     *
     * @ORM\Column(name="is_special", type="boolean", nullable=true)
     */
    private $isSpecial;

    /**
     * @var integer
     *
     * @ORM\Column(name="quantity", type="integer", nullable=true)
     */
    private $quantity;

    /**
     * created Time/Date
     *
     * @var \DateTime
     *
     * @ORM\Column(name="created_at", type="datetime", nullable=false)
     */
    protected $createdAt;

    /**
     * updated Time/Date
     *
     * @var \DateTime
     *
     * @ORM\Column(name="updated_at", type="datetime", nullable=false)
     */
    protected $updatedAt;

    /**
     * @var boolean
     *
     * @ORM\Column(name="seller", type="boolean", length=255, nullable=true)
     */
    private $seller;

    /**
     * Set createdAt
     *
     * @ORM\PrePersist
     */
    public function setCreatedAt()
    {
        $this->createdAt = new \DateTime();
        $this->updatedAt = new \DateTime();
    }

    /**
     * Get createdAt
     *
     * @return \DateTime
     */
    public function getCreatedAt()
    {
        return $this->createdAt;
    }

    /**
     * Set updatedAt
     *
     * @ORM\PreUpdate
     */
    public function setUpdatedAt()
    {
        $this->updatedAt = new \DateTime();
    }

    /**
     * Get updatedAt
     *
     * @return \DateTime
     */
    public function getUpdatedAt()
    {
        return $this->updatedAt;
    }


    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set title
     *
     * @param string $title
     *
     * @return Products
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

    /**
     * Get title
     *
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Set url
     *
     * @param string $url
     *
     * @return Products
     */
    public function setUrl($url)
    {
        $this->url = $url;

        return $this;
    }

    /**
     * Get url
     *
     * @return string
     */
    public function getUrl()
    {
        return $this->url;
    }

    /**
     * Set note
     *
     * @param string $note
     *
     * @return Products
     */
    public function setNote($note)
    {
        $this->note = $note;

        return $this;
    }

    /**
     * Get note
     *
     * @return string
     */
    public function getNote()
    {
        return $this->note;
    }

    /**
     * Set views
     *
     * @param integer $views
     *
     * @return Products
     */
    public function setViews($views)
    {
        $this->views = $views;

        return $this;
    }

    /**
     * Get views
     *
     * @return integer
     */
    public function getViews()
    {
        return $this->views;
    }

    /**
     * Set price
     *
     * @param integer $price
     *
     * @return Products
     */
    public function setPrice($price)
    {
        $this->price = $price;

        return $this;
    }

    /**
     * Get price
     *
     * @return integer
     */
    public function getPrice()
    {
        return $this->price;
    }

    /**
     * Set boughtPrice
     *
     * @param integer $boughtPrice
     *
     * @return Products
     */
    public function setBoughtPrice($boughtPrice)
    {
        $this->boughtPrice = $boughtPrice;

        return $this;
    }

    /**
     * Get boughtPrice
     *
     * @return integer
     */
    public function getBoughtPrice()
    {
        return $this->boughtPrice;
    }

    /**
     * Set isActive
     *
     * @param boolean $isActive
     *
     * @return Products
     */
    public function setIsActive($isActive)
    {
        $this->isActive = $isActive;

        return $this;
    }

    /**
     * Get isActive
     *
     * @return boolean
     */
    public function getIsActive()
    {
        return $this->isActive;
    }

    /**
     * Set isAccessory
     *
     * @param boolean $isAccessory
     *
     * @return Products
     */
    public function setIsAccessory($isAccessory)
    {
        $this->isAccessory = $isAccessory;

        return $this;
    }

    /**
     * Get isAccessory
     *
     * @return boolean
     */
    public function getIsAccessory()
    {
        return $this->isAccessory;
    }

    /**
     * Set quantity
     *
     * @param integer $quantity
     *
     * @return Products
     */
    public function setQuantity($quantity)
    {
        $this->quantity = $quantity;

        return $this;
    }

    /**
     * Get quantity
     *
     * @return integer
     */
    public function getQuantity()
    {
        return $this->quantity;
    }


    /**
     * Set details
     *
     * @param \Web\AdminBundle\Entity\ProductDetails $details
     *
     * @return Products
     */
    public function setDetails(\Web\AdminBundle\Entity\ProductDetails $details = null)
    {
        $this->details = $details;

        return $this;
    }

    /**
     * Get details
     *
     * @return \Web\AdminBundle\Entity\ProductDetails
     */
    public function getDetails()
    {
        return $this->details;
    }

    /**
     * Add image
     *
     * @param \Web\AdminBundle\Entity\ProductImages $image
     *
     * @return Products
     */
    public function addImage(\Web\AdminBundle\Entity\ProductImages $image)
    {
        $this->images[] = $image;

        return $this;
    }

    /**
     * Remove image
     *
     * @param \Web\AdminBundle\Entity\ProductImages $image
     */
    public function removeImage(\Web\AdminBundle\Entity\ProductImages $image)
    {
        $this->images->removeElement($image);
    }

    /**
     * Get images
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getImages()
    {
        return $this->images;
    }

    /**
     * Add cart
     *
     * @param \Web\AdminBundle\Entity\Cart $cart
     *
     * @return Products
     */
    public function addCart(\Web\AdminBundle\Entity\Cart $cart)
    {
        $this->cart[] = $cart;

        return $this;
    }

    /**
     * Remove cart
     *
     * @param \Web\AdminBundle\Entity\Cart $cart
     */
    public function removeCart(\Web\AdminBundle\Entity\Cart $cart)
    {
        $this->cart->removeElement($cart);
    }

    /**
     * Get cart
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getCart()
    {
        return $this->cart;
    }

    /**
     * Set isSpecial
     *
     * @param boolean $isSpecial
     *
     * @return Products
     */
    public function setIsSpecial($isSpecial)
    {
        $this->isSpecial = $isSpecial;

        return $this;
    }

    /**
     * Get isSpecial
     *
     * @return boolean
     */
    public function getIsSpecial()
    {
        return $this->isSpecial;
    }

    /**
     * Set accessoryCategory
     *
     * @param \Web\AdminBundle\Entity\AccessoryCategory $accessoryCategory
     *
     * @return Products
     */
    public function setAccessoryCategory(\Web\AdminBundle\Entity\AccessoryCategory $accessoryCategory = null)
    {
        $this->accessoryCategory = $accessoryCategory;

        return $this;
    }

    /**
     * Get accessoryCategory
     *
     * @return \Web\AdminBundle\Entity\AccessoryCategory
     */
    public function getAccessoryCategory()
    {
        return $this->accessoryCategory;
    }

    /**
     * Set oldPrice
     *
     * @param integer $oldPrice
     *
     * @return Products
     */
    public function setOldPrice($oldPrice)
    {
        $this->oldPrice = $oldPrice;

        return $this;
    }

    /**
     * Get oldPrice
     *
     * @return integer
     */
    public function getOldPrice()
    {
        return $this->oldPrice;
    }

    /**
     * Set serial
     *
     * @param string $serial
     *
     * @return Products
     */
    public function setSerial($serial)
    {
        $this->serial = $serial;

        return $this;
    }

    /**
     * Get serial
     *
     * @return string
     */
    public function getSerial()
    {
        return $this->serial;
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->models = new \Doctrine\Common\Collections\ArrayCollection();
        $this->images = new \Doctrine\Common\Collections\ArrayCollection();
        $this->cart = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add model
     *
     * @param \Web\AdminBundle\Entity\Models $model
     *
     * @return Products
     */
    public function addModel(\Web\AdminBundle\Entity\Models $model)
    {
        $this->models[] = $model;

        return $this;
    }

    /**
     * Remove model
     *
     * @param \Web\AdminBundle\Entity\Models $model
     */
    public function removeModel(\Web\AdminBundle\Entity\Models $model)
    {
        $this->models->removeElement($model);
    }

    /**
     * Get models
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getModels()
    {
        return $this->models;
    }

    /**
     * Set seller
     *
     * @param boolean $seller
     *
     * @return Products
     */
    public function setSeller($seller)
    {
        $this->seller = $seller;

        return $this;
    }

    /**
     * Get seller
     *
     * @return boolean
     */
    public function getSeller()
    {
        return $this->seller;
    }

我正在像这样添加新的:

$em = $this->getDoctrine()->getManager();
$products = new Products();
$article->setTitle('here is the title');
....
$em->persist($products);
$em->flush();

这应该将 id 设置为 488,但它试图将其设置为 1。我不知道为什么?可能是一些缓存?但是 php bin/console cache:clear 并没有改变任何东西。

【问题讨论】:

  • 如何添加新记录?也许与您的 Symfony 应用程序有关?如果是这样,您需要显示实体。
  • 添加有关迁移的详细信息以及如何将新记录添加到数据库
  • 我编辑了我的问题。
  • 是mysql数据库吗?如果是这样,您是否检查了主键的自动增量值?

标签: postgresql doctrine-orm symfony


【解决方案1】:

我认为其他人认为这不是 symfony 或教义的问题,而是它的 postgresql 常见问题,您可以查看solution here

确保您使用的是相同版本,我不确定此行为是否因版本而异。

【讨论】:

    【解决方案2】:

    您可以像这样手动更改自动增量的当前值:

    ALTER TABLE products AUTO_INCREMENT=501;

    【讨论】:

      【解决方案3】:

      嗨,当我像他们说的序列一样导入我的数据库时,我遇到了同样的问题。
      但是因为我有超过 100 个表我不能一个一个地重置序列所以我发现这个查询它为你的所有表创建一个 sql 查询来更新最大 ID 的序列,你只需要复制结果并执行它

      SELECT 'SELECT SETVAL(' ||
         quote_literal(quote_ident(PGT.schemaname) || '.' || quote_ident(S.relname)) ||
         ', COALESCE(MAX(' ||quote_ident(C.attname)|| '), 1) ) FROM ' ||
         quote_ident(PGT.schemaname)|| '.'||quote_ident(T.relname)|| ';'
       FROM pg_class AS S,
          pg_depend AS D,
          pg_class AS T,
          pg_attribute AS C,
          pg_tables AS PGT
      WHERE S.relkind = 'S'
         AND S.oid = D.objid
         AND D.refobjid = T.oid
         AND D.refobjid = C.attrelid
         AND D.refobjsubid = C.attnum
         AND T.relname = PGT.tablename
      ORDER BY S.relname;
      

      【讨论】:

        【解决方案4】:

        具有自动策略和 Postgres 的 Doctrine 使用序列。

        可能在导出/导入 db 时序列值已丢失。 识别出你的ID使用的序列并尝试执行:

        ALTER SEQUENCE sequence_name RESTART WITH your_next_free_id;
        

        【讨论】:

        • 谢谢,但是我怎样才能从学说中获取序列名称,对不起,我以前从未使用过 postgresql。
        • 您需要通过直接访问 postgres 来完成,例如使用 psql - postgres 控制台
        • 默认的命名策略是table_name_ + field_name + _seq,以你的例子来说应该是products_id_seq
        • 这是一个列出所有postgresql序列的查询stackoverflow.com/a/1611680/2451983
        猜你喜欢
        • 2017-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-08
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        • 2022-07-24
        相关资源
        最近更新 更多