Symfony / Doctrine - Id已经存在

时间:2017-03-02 05:34:20

标签: postgresql doctrine-orm symfony

我有问题。我将网站移动到另一个托管(所以我导出数据库并导入到新托管)。 但是现在,当我尝试将新记录添加到数据库时。我有一个错误,例如 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并没有改变任何东西。

4 个答案:

答案 0 :(得分:2)

使用auto策略和Postgres的Doctrine使用序列。

导出/导入db时,序列值可能已丢失。 确定您的ID使用的序列并尝试执行:

ALTER SEQUENCE sequence_name RESTART WITH your_next_free_id;

答案 1 :(得分:0)

嗨我在导入我的数据库时遇到了同样的问题,问题就像他们说序列一样 但是因为我有超过100个表我无法一个一个地重置序列所以我发现这个查询它为你的所有表创建一个sql查询来通过max 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;

答案 2 :(得分:0)

您可以手动更改自动增量的当前值

ALTER TABLE products AUTO_INCREMENT=501;

答案 3 :(得分:0)

我认为正如其他人所说的不是symfony或doctrine的问题,而是postgresql的常见问题,你可以查看solution here

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