pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Problem with serial counters
3+ messages / 3 participants
[nested] [flat]

* Problem with serial counters
@ 2011-02-24 21:09  Jacques Lebrun <jlebrun@cplus.org>
  0 siblings, 2 replies; 3+ messages in thread

From: Jacques Lebrun @ 2011-02-24 21:09 UTC (permalink / raw)
  To: pgsql-sql

When I install my application, I use a SQL script to create the different tables.
Most of my tables start with a Serial column called RowId.
In my setup script I insert a few records into some tables, providing all fields, including the RowId (1, 2, 3, etc).

The problem I have is that when my user tries to insert a new record without the RowId field,
Postgres complains that it finds a duplicate key.  
I guess when I do an insert with all the fields (including the RowId), Postgres does not increment the serial counter.

I cannot remove the RowId field from the Insert of my setup script because this setup script is also used by customers using MySQL.

What can I do to force PostGres to update the internat serial counter when I do an insert with a specified value for the serial?



Jacques Lebrun
Web Mobile Inc
819-563-0133
JLebrun@cplus.org

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Problem with serial counters
@ 2011-02-25 18:47  Vibhor Kumar <vibhor.aim@gmail.com>
  parent: Jacques Lebrun <jlebrun@cplus.org>
  1 sibling, 0 replies; 3+ messages in thread

From: Vibhor Kumar @ 2011-02-25 18:47 UTC (permalink / raw)
  To: Jacques Lebrun <jlebrun@cplus.org>; +Cc: pgsql-sql


On Feb 25, 2011, at 2:39 AM, Jacques Lebrun wrote:
> What can I do to force PostGres to update the internat serial counter when I do an insert with a specified value for the serial?
>  

After inserting the all the Data, use ALTER SEQUENCE Command:
http://www.postgresql.org/docs/8.4/static/sql-altersequence.html

Thanks & Regards,
Vibhor




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Problem with serial counters
@ 2011-03-01 20:29  Richard Huxton <dev@archonet.com>
  parent: Jacques Lebrun <jlebrun@cplus.org>
  1 sibling, 0 replies; 3+ messages in thread

From: Richard Huxton @ 2011-03-01 20:29 UTC (permalink / raw)
  To: Jacques Lebrun <jlebrun@cplus.org>; +Cc: pgsql-sql

On 24/02/11 21:09, Jacques Lebrun wrote:
> I guess when I do an insert with all the fields (including the RowId),
> Postgres does not increment the serial counter.
> I cannot remove the RowId field from the Insert of my setup script
> because this setup script is also used by customers using MySQL.

Apart from Vibhor's suggestion (which is the typical way PG does it) you 
can also set the auto field to DEFAULT:

INSERT INTO mytable (rowid, other) VALUES (DEFAULT,'a'), (DEFAULT,'b');

This should work on either system.

-- 
   Richard Huxton
   Archonet Ltd



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2011-03-01 20:29 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-02-24 21:09 Problem with serial counters Jacques Lebrun <jlebrun@cplus.org>
2011-02-25 18:47 ` Vibhor Kumar <vibhor.aim@gmail.com>
2011-03-01 20:29 ` Richard Huxton <dev@archonet.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox