Symfony / Doctrine- ID已经存在



我有问题。我将网站移至另一个托管(因此我导出了DB并进口到新托管)。但是现在,当我尝试将新记录添加到数据库时。我有一个错误STN,例如 ID 1已经存在。,但我在表中有几乎500个记录。

我该怎么办?

对不起,我应该提供更多信息:

我通过phppgadmin导出并导入数据库,我没有使用迁移捆绑包。

这是我的实体:

class Products
{
    /**
     * @var int
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @var string
     *
     * @ORMColumn(name="serial_number", type="string", length=255, nullable=true)
     */
    private $serial;
    /**
     * @var string
     * @Expose
     * @ORMColumn(name="title", type="string", length=255)
     */
    private $title;
    /**
     * @var string
     * @Expose
     * @ORMColumn(name="url", type="string", length=255)
     */
    private $url;
    /**
     * @var string
     * @ORMColumn(name="note", type="text", nullable=true)
     */
    private $note;
    /**
     * @var int
     *
     * @ORMColumn(name="views", type="bigint", nullable=true)
     */
    private $views;
    /**
     * @ORMManyToMany(targetEntity="Models")
     * @ORMJoinTable(name="products_models",
     *      joinColumns={@ORMJoinColumn(name="product_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORMJoinColumn(name="model_id", referencedColumnName="id")}
     *      )
     */
    private $models;
    /**
     * @ORMOneToOne(targetEntity="ProductDetails", cascade={"persist", "remove"})
     * @ORMJoinColumn(name="details_id", referencedColumnName="id")
     */
    private $details;
    /**
     * @var File
     * @Expose
     * @ORMOneToMany(targetEntity="ProductImages", mappedBy="product", cascade={"persist", "remove"})
     * @ORMOrderBy({"id" = "ASC"})
     *
     */
    private $images;
    /**
     * @var File
     *
     * @ORMOneToMany(targetEntity="Cart", mappedBy="productId", cascade={"persist"})
     *
     */
    private $cart;

    /**
     * @var string
     *
     * @ORMColumn(name="price", type="integer", length=255, nullable=true)
     */
    private $price;
    /**
     * @var string
     *
     * @ORMColumn(name="bought_price", type="integer", length=255, nullable=true)
     */
    private $boughtPrice;
    /**
     * @var string
     *
     * @ORMColumn(name="old_price", type="integer", length=255, nullable=true)
     */
    private $oldPrice;

    /**
     * @var bool
     *
     * @ORMColumn(name="is_active", type="boolean", nullable=true)
     */
    private $isActive;
    /**
     * @var bool
     *
     * @ORMColumn(name="is_accessory", type="boolean", nullable=true)
     */
    private $isAccessory;
    /**
     * @ORMManyToOne(targetEntity="AccessoryCategory")
     * @ORMJoinColumn(name="accessory_category_id", referencedColumnName="id")
     */
    private $accessoryCategory;
    /**
     * @var bool
     *
     * @ORMColumn(name="is_special", type="boolean", nullable=true)
     */
    private $isSpecial;
    /**
     * @var integer
     *
     * @ORMColumn(name="quantity", type="integer", nullable=true)
     */
    private $quantity;
    /**
     * created Time/Date
     *
     * @var DateTime
     *
     * @ORMColumn(name="created_at", type="datetime", nullable=false)
     */
    protected $createdAt;
    /**
     * updated Time/Date
     *
     * @var DateTime
     *
     * @ORMColumn(name="updated_at", type="datetime", nullable=false)
     */
    protected $updatedAt;
    /**
     * @var boolean
     *
     * @ORMColumn(name="seller", type="boolean", length=255, nullable=true)
     */
    private $seller;
    /**
     * Set createdAt
     *
     * @ORMPrePersist
     */
    public function setCreatedAt()
    {
        $this->createdAt = new DateTime();
        $this->updatedAt = new DateTime();
    }
    /**
     * Get createdAt
     *
     * @return DateTime
     */
    public function getCreatedAt()
    {
        return $this->createdAt;
    }
    /**
     * Set updatedAt
     *
     * @ORMPreUpdate
     */
    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 WebAdminBundleEntityProductDetails $details
     *
     * @return Products
     */
    public function setDetails(WebAdminBundleEntityProductDetails $details = null)
    {
        $this->details = $details;
        return $this;
    }
    /**
     * Get details
     *
     * @return WebAdminBundleEntityProductDetails
     */
    public function getDetails()
    {
        return $this->details;
    }
    /**
     * Add image
     *
     * @param WebAdminBundleEntityProductImages $image
     *
     * @return Products
     */
    public function addImage(WebAdminBundleEntityProductImages $image)
    {
        $this->images[] = $image;
        return $this;
    }
    /**
     * Remove image
     *
     * @param WebAdminBundleEntityProductImages $image
     */
    public function removeImage(WebAdminBundleEntityProductImages $image)
    {
        $this->images->removeElement($image);
    }
    /**
     * Get images
     *
     * @return DoctrineCommonCollectionsCollection
     */
    public function getImages()
    {
        return $this->images;
    }
    /**
     * Add cart
     *
     * @param WebAdminBundleEntityCart $cart
     *
     * @return Products
     */
    public function addCart(WebAdminBundleEntityCart $cart)
    {
        $this->cart[] = $cart;
        return $this;
    }
    /**
     * Remove cart
     *
     * @param WebAdminBundleEntityCart $cart
     */
    public function removeCart(WebAdminBundleEntityCart $cart)
    {
        $this->cart->removeElement($cart);
    }
    /**
     * Get cart
     *
     * @return DoctrineCommonCollectionsCollection
     */
    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 WebAdminBundleEntityAccessoryCategory $accessoryCategory
     *
     * @return Products
     */
    public function setAccessoryCategory(WebAdminBundleEntityAccessoryCategory $accessoryCategory = null)
    {
        $this->accessoryCategory = $accessoryCategory;
        return $this;
    }
    /**
     * Get accessoryCategory
     *
     * @return WebAdminBundleEntityAccessoryCategory
     */
    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 DoctrineCommonCollectionsArrayCollection();
        $this->images = new DoctrineCommonCollectionsArrayCollection();
        $this->cart = new DoctrineCommonCollectionsArrayCollection();
    }
    /**
     * Add model
     *
     * @param WebAdminBundleEntityModels $model
     *
     * @return Products
     */
    public function addModel(WebAdminBundleEntityModels $model)
    {
        $this->models[] = $model;
        return $this;
    }
    /**
     * Remove model
     *
     * @param WebAdminBundleEntityModels $model
     */
    public function removeModel(WebAdminBundleEntityModels $model)
    {
        $this->models->removeElement($model);
    }
    /**
     * Get models
     *
     * @return DoctrineCommonCollectionsCollection
     */
    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不会更改任何内容。

与自动策略和Postgres一起使用序列。

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

ALTER SEQUENCE sequence_name RESTART WITH your_next_free_id;

嗨,当我导入数据库(如他们说的序列)时,我也遇到了同样的问题。
但是,由于我有100个以上的表,所以我无法一个一个序列重置序列它

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;

我认为其他建议不是Symfony或Doctrine的问题,而是其PostgreSQL共同问题,您可以在此处检查解决方案

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

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

ALTER TABLE products AUTO_INCREMENT=501;

相关内容

  • 没有找到相关文章

最新更新