2

I have a problem. I moved website to another hosting(so I exported DB and imported to new hosting). But now, when I trying to add new record to database. I have an error stn like ID 1 already exist. But I have almost 500 records in the table.

What can I do to fix it?

Sorry I've should provide more information:

I exported and imported database through phppgadmin I didn't use migrations bundle.

Here is my entity:

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;
    }

I'm adding new like this:

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

this should set id to like 488 but it's trying to set it to 1. And I don't know why? Might be some cache? But php bin/console cache:clear doesn't change anything.

4 Answers4

3

Doctrine use sequence with the auto strategy and Postgres.

Maybe sequence values has been lost when export/import db. Identify the sequence used by your ID and try to execute :

ALTER SEQUENCE sequence_name RESTART WITH your_next_free_id;
j-guyon
  • 1,693
  • 16
  • 29
  • Thanks, but how can I get sequence name from doctrine, sorry I never used postgresql before. –  Mar 02 '17 at 09:22
  • you need to do by direct access to postgres, for example by using `psql` - postgres console – Tomasz Madeyski Mar 02 '17 at 09:54
  • The default naming strategy is `table_name_` + `field_name` + `_seq`, with your example, it should be something like `products_id_seq` – j-guyon Mar 02 '17 at 09:59
  • Here is a query to list all postgresql sequences http://stackoverflow.com/a/1611680/2451983 – j-guyon Mar 02 '17 at 10:13
1

hi i have got the same problem when i Import my database the probléme s like they say sequence.
But because i have more than 100 table i can't reset the sequence one by one So i Found this query it create for all your table an sql query to update the sequence by the max Id, you have just to copy the result and execute it

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;
Barkati.med
  • 530
  • 4
  • 11
1

I think as other have suggested its not the issue with symfony or doctrine , instead its postgresql common issue, you can check a solution here

Make sure you are using same version, I am not sure if this behavior differs from version to version.

sakhunzai
  • 11,985
  • 19
  • 85
  • 142
0

You can change the current value of the auto increment manually like this :

ALTER TABLE products AUTO_INCREMENT=501;