DDC-2001: Problem nextval sequence persist many object #2526

Closed
opened 2026-01-22 13:55:54 +01:00 by admin · 5 comments
Owner

Originally created by @doctrinebot on GitHub (Aug 29, 2012).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user sandrocandido:

Seems to be a bug doctrine.

Scenario: You cart table and Product table when trying to persist the products do not set the last sequence postgres. In the example cart will id = 1
products id = 1 ... 5 but the sequence of the final product value is 2 and not 5.
If I run the script again I get the following error.

PDOException: SQLSTATE[23505]: Unique violation: 7 ERROR: duplicate key value violates unique constraint "product*pkey" DETAIL: Key (id)=(2) already exists. in /var/www/codeigniter/scclocal/application/third*party/doctrine-orm/Doctrine/DBAL/Statement.php on line 131

Sample code:

        $cart = new cart\models\Cart;

        $cart->em->getConnection()->beginTransaction();

        $cart->setDescription('My Cart');

        try {
            //Persist Cart
            $cart->em->persist($cart);
            $cart->em->flush();
        } catch(Exception $e) {
            $cart->em->getConnection()->rollback();
            $cart->em->close();
            throw $e;
        }
        //Product
        for($i=0; $i < 5; $i<ins></ins>){
            $product = new cart\models\Product;
            $product->setName('Name product '.$i);
            $product->setDescription('Description product '.$i);
            $product->setCart($cart);
            $cart->getProducts()->add($product); //Add products in cart
            $product->em->persist($product); //Persist Products
        }

        try {
            $cart->em->persist($cart);
            $cart->em->flush();
            $cart->em->getConnection()->commit();
        } catch(Exception $e) {
            $cart->em->getConnection()->rollback();
            $cart->em->close();
            throw $e;
        }
        die ('success save);

EchoSQLLogger Cart:

SELECT NEXTVAL('cart*id*seq') INSERT INTO cart (id, description) VALUES (?, ?)

array
  1 => int 1
  2 => string 'My Cart' (length=7)

EchoSQLLogger Products( Note that the second interaction forward select nextval was not generated. ) :

SELECT NEXTVAL('product*id_seq') INSERT INTO product (id, name, description, cart*id) VALUES (?, ?, ?, ?)

array
  1 => int 1
  2 => string 'Name product 0' (length=14)
  3 => string 'Description product 0' (length=21)
  4 => int 1

INSERT INTO product (id, name, description, cart_id) VALUES (?, ?, ?, ?)

array
  1 => int 2
  2 => string 'Name product 1' (length=14)
  3 => string 'Description product 1' (length=21)
  4 => int 1

INSERT INTO product (id, name, description, cart_id) VALUES (?, ?, ?, ?)

array
  1 => int 3
  2 => string 'Name product 2' (length=14)
  3 => string 'Description product 2' (length=21)
  4 => int 1

INSERT INTO product (id, name, description, cart_id) VALUES (?, ?, ?, ?)

array
  1 => int 4
  2 => string 'Name product 3' (length=14)
  3 => string 'Description product 3' (length=21)
  4 => int 1

INSERT INTO product (id, name, description, cart_id) VALUES (?, ?, ?, ?)

array
  1 => int 5
  2 => string 'Name product 4' (length=14)
  3 => string 'Description product 4' (length=21)
  4 => int 1

Entity Cart:

<?php
namespace cart\models;

/****
 * @Entity
 * @Table(name="cart")
 */
use models\MY_Model,
    Doctrine\Common\Collections\ArrayCollection;


class Cart extends MY_Model {

    /****
     * @Id
     * @Column(type="integer", nullable=false)
     * @GeneratedValue(strategy="SEQUENCE")
     * @SequenceGenerator(sequenceName="cart*id*seq", initialValue=1, allocationSize=100)
     */
    private $id;

    /****
     * @Column(type="string", length=200, nullable=true)
     */
    private $description;


    /****
     * @OneToMany(targetEntity="Product", mappedBy="cart", cascade={"all"})
     */
    private $products;

    public function **construct() {
        parent::**construct();
        $this->products = new ArrayCollection();
    }

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

    /****
     *
     * @param $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /****
     *
     * @return
     */
    public function getDescription()
    {
        return $this->description;
    }

    /****
     *
     * @param $descricao
     */
    public function setDescription($description)
    {
        $this->description = $description;
    }

    /****
     *
     * @return
     */
    public function getProducts()
    {
        return $this->products;
    }

    /****
     *
     * @param $categoria
     */
    public function setProducts(Products $products)
    {
        $this->products = $products;
    }
}
?>

Entity Product:

<?php
namespace cart\models;

/****
 * @Entity
 * @Table(name="product")
 */
use models\MY_Model,
    Doctrine\Common\Collections\ArrayCollection;

class Product extends MY_Model {

    /****
     * @Id
     * @Column(type="integer", nullable=false)
     * @GeneratedValue(strategy="SEQUENCE")
     * @SequenceGenerator(sequenceName="product*id*seq", initialValue=1, allocationSize=100)
     */
    private $id;

    /****
     * @Column(type="string", length=100)
     */
    private $name;

    /****
     * @Column(type="string", length=200)
     */
    private $description;


    /****
     * @link http://www.doctrine-project.org/docs/orm/2.0/en/reference/association-mapping.html#one-to-many-bidirectional
     *
     * @ManyToOne(targetEntity="Cart", inversedBy="product")
     * @JoinColumn(name="cart_id", referencedColumnName="id")
     */
    protected $cart;


    public function **construct(){
        parent::**construct();
    }

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

    /****
     *
     * @param $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /****
     *
     * @return
     */
    public function getName()
    {
        return $this->name;
    }

    /****
     *
     * @param $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /****
     *
     * @return
     */
    public function getDescription()
    {
        return $this->description;
    }

    /****
     *
     * @param $description
     */
    public function setDescription($description)
    {
        $this->description = $description;
    }

    /****
     *
     * @return
     */
    public function getCart()
    {
        return $this->cart;
    }

    /****
     *
     * @param $cart
     */
    public function setCart(Cart $cart)
    {
        $this->cart = $cart;
    }
}
?>

Attachment application sample

Originally created by @doctrinebot on GitHub (Aug 29, 2012). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user sandrocandido: Seems to be a bug doctrine. Scenario: You cart table and Product table when trying to persist the products do not set the last sequence postgres. In the example cart will id = 1 products id = 1 ... 5 but the sequence of the final product value is 2 and not 5. If I run the script again I get the following error. ``` PDOException: SQLSTATE[23505]: Unique violation: 7 ERROR: duplicate key value violates unique constraint "product*pkey" DETAIL: Key (id)=(2) already exists. in /var/www/codeigniter/scclocal/application/third*party/doctrine-orm/Doctrine/DBAL/Statement.php on line 131 ``` Sample code: ``` $cart = new cart\models\Cart; $cart->em->getConnection()->beginTransaction(); $cart->setDescription('My Cart'); try { //Persist Cart $cart->em->persist($cart); $cart->em->flush(); } catch(Exception $e) { $cart->em->getConnection()->rollback(); $cart->em->close(); throw $e; } //Product for($i=0; $i < 5; $i<ins></ins>){ $product = new cart\models\Product; $product->setName('Name product '.$i); $product->setDescription('Description product '.$i); $product->setCart($cart); $cart->getProducts()->add($product); //Add products in cart $product->em->persist($product); //Persist Products } try { $cart->em->persist($cart); $cart->em->flush(); $cart->em->getConnection()->commit(); } catch(Exception $e) { $cart->em->getConnection()->rollback(); $cart->em->close(); throw $e; } die ('success save); ``` EchoSQLLogger Cart: ``` SELECT NEXTVAL('cart*id*seq') INSERT INTO cart (id, description) VALUES (?, ?) array 1 => int 1 2 => string 'My Cart' (length=7) ``` EchoSQLLogger Products( Note that the second interaction forward select nextval was not generated. ) : ``` SELECT NEXTVAL('product*id_seq') INSERT INTO product (id, name, description, cart*id) VALUES (?, ?, ?, ?) array 1 => int 1 2 => string 'Name product 0' (length=14) 3 => string 'Description product 0' (length=21) 4 => int 1 INSERT INTO product (id, name, description, cart_id) VALUES (?, ?, ?, ?) array 1 => int 2 2 => string 'Name product 1' (length=14) 3 => string 'Description product 1' (length=21) 4 => int 1 INSERT INTO product (id, name, description, cart_id) VALUES (?, ?, ?, ?) array 1 => int 3 2 => string 'Name product 2' (length=14) 3 => string 'Description product 2' (length=21) 4 => int 1 INSERT INTO product (id, name, description, cart_id) VALUES (?, ?, ?, ?) array 1 => int 4 2 => string 'Name product 3' (length=14) 3 => string 'Description product 3' (length=21) 4 => int 1 INSERT INTO product (id, name, description, cart_id) VALUES (?, ?, ?, ?) array 1 => int 5 2 => string 'Name product 4' (length=14) 3 => string 'Description product 4' (length=21) 4 => int 1 ``` Entity Cart: ``` <?php namespace cart\models; /**** * @Entity * @Table(name="cart") */ use models\MY_Model, Doctrine\Common\Collections\ArrayCollection; class Cart extends MY_Model { /**** * @Id * @Column(type="integer", nullable=false) * @GeneratedValue(strategy="SEQUENCE") * @SequenceGenerator(sequenceName="cart*id*seq", initialValue=1, allocationSize=100) */ private $id; /**** * @Column(type="string", length=200, nullable=true) */ private $description; /**** * @OneToMany(targetEntity="Product", mappedBy="cart", cascade={"all"}) */ private $products; public function **construct() { parent::**construct(); $this->products = new ArrayCollection(); } /**** * * @return */ public function getId() { return $this->id; } /**** * * @param $id */ public function setId($id) { $this->id = $id; } /**** * * @return */ public function getDescription() { return $this->description; } /**** * * @param $descricao */ public function setDescription($description) { $this->description = $description; } /**** * * @return */ public function getProducts() { return $this->products; } /**** * * @param $categoria */ public function setProducts(Products $products) { $this->products = $products; } } ?> ``` Entity Product: ``` <?php namespace cart\models; /**** * @Entity * @Table(name="product") */ use models\MY_Model, Doctrine\Common\Collections\ArrayCollection; class Product extends MY_Model { /**** * @Id * @Column(type="integer", nullable=false) * @GeneratedValue(strategy="SEQUENCE") * @SequenceGenerator(sequenceName="product*id*seq", initialValue=1, allocationSize=100) */ private $id; /**** * @Column(type="string", length=100) */ private $name; /**** * @Column(type="string", length=200) */ private $description; /**** * @link http://www.doctrine-project.org/docs/orm/2.0/en/reference/association-mapping.html#one-to-many-bidirectional * * @ManyToOne(targetEntity="Cart", inversedBy="product") * @JoinColumn(name="cart_id", referencedColumnName="id") */ protected $cart; public function **construct(){ parent::**construct(); } /**** * * @return */ public function getId() { return $this->id; } /**** * * @param $id */ public function setId($id) { $this->id = $id; } /**** * * @return */ public function getName() { return $this->name; } /**** * * @param $name */ public function setName($name) { $this->name = $name; } /**** * * @return */ public function getDescription() { return $this->description; } /**** * * @param $description */ public function setDescription($description) { $this->description = $description; } /**** * * @return */ public function getCart() { return $this->cart; } /**** * * @param $cart */ public function setCart(Cart $cart) { $this->cart = $cart; } } ?> ``` Attachment application sample
admin added the Bug label 2026-01-22 13:55:54 +01:00
admin closed this issue 2026-01-22 13:55:55 +01:00
Author
Owner

@doctrinebot commented on GitHub (Aug 29, 2012):

Comment created by @beberlei:

But the SQL Log correctly shows 1,..,5 in case of the INSERT statements for products. Where is the unique id clash happening?

@doctrinebot commented on GitHub (Aug 29, 2012): Comment created by @beberlei: But the SQL Log correctly shows 1,..,5 in case of the INSERT statements for products. Where is the unique id clash happening?
Author
Owner

@doctrinebot commented on GitHub (Aug 29, 2012):

Comment created by sandrocandido:

It happens that for each insert must have a nextval to generate the unique id. Log in to run the script the first time the insert works but if you run the script again next products are not generated 6 ... 10 of the error occurring.

PDOException: SQLSTATE[23505]: Unique violation: 7 ERROR: duplicate key value violates unique constraint "product_pkey" DETAIL: Key (id)=(2) already exists. in /var/www/codeigniter/bugcodeigniterdoctrine/application/third_party/doctrine-orm/Doctrine/DBAL/Statement.php on line 131

@doctrinebot commented on GitHub (Aug 29, 2012): Comment created by sandrocandido: It happens that for each insert must have a nextval to generate the unique id. Log in to run the script the first time the insert works but if you run the script again next products are not generated 6 ... 10 of the error occurring. PDOException: SQLSTATE[23505]: Unique violation: 7 ERROR: duplicate key value violates unique constraint "product_pkey" DETAIL: Key (id)=(2) already exists. in /var/www/codeigniter/bugcodeigniterdoctrine/application/third_party/doctrine-orm/Doctrine/DBAL/Statement.php on line 131
Author
Owner

@doctrinebot commented on GitHub (Aug 30, 2012):

Comment created by sandrocandido:

Benjamin you could understand the problem now?

@doctrinebot commented on GitHub (Aug 30, 2012): Comment created by sandrocandido: Benjamin you could understand the problem now?
Author
Owner

@doctrinebot commented on GitHub (Oct 6, 2012):

Comment created by @beberlei:

Its not a bug with Doctrine but your code, your sequence is defined wrong:

CREATE SEQUENCE product*id*seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

If you increment by 100, then the sequence has to increment by 100. This is a low level db vendor feature.

@doctrinebot commented on GitHub (Oct 6, 2012): Comment created by @beberlei: Its not a bug with Doctrine but your code, your sequence is defined wrong: ``` CREATE SEQUENCE product*id*seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1; ``` If you increment by 100, then the sequence has to increment by 100. This is a low level db vendor feature.
Author
Owner

@doctrinebot commented on GitHub (Oct 6, 2012):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Oct 6, 2012): Issue was closed with resolution "Invalid"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#2526