Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql Symfony/条令-Id已存在_Postgresql_Doctrine Orm_Symfony - Fatal编程技术网

Postgresql Symfony/条令-Id已存在

Postgresql Symfony/条令-Id已存在,postgresql,doctrine-orm,symfony,Postgresql,Doctrine Orm,Symfony,我有个问题。我将网站移动到另一个主机(所以我导出了DB并导入到新主机)。 但现在,当我试图向数据库添加新记录时。我有一个类似于ID 1的错误stn已经存在。但是我在表中有将近500条记录 我能做些什么来修复它 抱歉,我应该提供更多信息: 我通过phppgadmin导出和导入数据库,但我没有使用迁移包 这是我的实体: class Products { /** * @var int * * @ORM\Column(name="id", type="integer

我有个问题。我将网站移动到另一个主机(所以我导出了DB并导入到新主机)。 但现在,当我试图向数据库添加新记录时。我有一个类似于ID 1的错误stn已经存在。但是我在表中有将近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不会改变任何东西。

原则使用自动策略和Postgres的顺序

导出/导入数据库时可能丢失了序列值。 识别ID使用的序列并尝试执行:

ALTER SEQUENCE sequence_name RESTART WITH your_next_free_id;

嗨,我在导入数据库时遇到了同样的问题,问题就像他们说的顺序一样。
但是因为我有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;

您可以手动更改自动增量的当前值,如下所示:


ALTER TABLE products AUTO_INCREMENT=501

我认为,正如其他人所建议的,这不是symfony或条令的问题,而是postgresql的常见问题,您可以检查


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

如何添加新记录?也许是你的Symfony应用程序?如果是这样,你需要显示实体。添加有关迁移的详细信息+如何将新记录添加到数据库我编辑了我的问题。它是mysql数据库吗?如果是这样的话,你检查过主键的自动递增值了吗?谢谢,但是我怎样才能从条令中获得序列名,对不起,我以前从未使用过postgresql。你需要直接访问postgres,例如,通过使用
psql
-postgres控制台,默认的命名策略是
table\u name\u
+
field\u name
+
\u seq
,以您的例子来说,它应该类似于
products\u id\u seq
,这里是一个列出所有postgresql序列的查询