Possible Duplicate:
is there a way to avoid calling nextval() if the insert fails in PostgreSQL?
Rails – Postgres does not re-use deleted ids but MySql does?
I have very simple database with PostgreSQL 9.2.1. When i insert something with Doctrine2 by using this method:
$em->persist($entity);
$em->flush();
If I get an error
-means record NOT inserted-
it still adds 1 to my primary key. So if i refresh the page 50 times, it says auto increment value is set to 50. How can I prevent this? If record not insterted than why primary key increases. This is very critical because it uses indexes for nothing and the number keep going bigger.
I have no idea what it came from and why, so, i did not tried anything. Very stuck at this point.
Doctrine2 uses transactions to complete it’s tasks.
Each time a Doctrine flushes its changes to the database, it opens a transaction, each record is prepared and then the commit will action the changes all at once.
During this process, the records that require an
auto_incrementID are assigned. If the record then fails during the commit phase, the records aren’t added, but the auto increment has issued that ID.You can read more about Transactions here: http://www.postgresql.org/docs/current/static/tutorial-transactions.html
Note: This is also the same for MySQL and I’m sure the same for many other RDBMS.