Skip site navigation (1) Skip section navigation (2)

Re: Make an id field max(id)+1 rather than SERIAL

From: Oliver Elphick <olly(at)lfix(dot)co(dot)uk>
To: Rory Campbell-Lange <rory(at)campbell-lange(dot)net>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Make an id field max(id)+1 rather than SERIAL
Date: 2002-09-19 16:59:43
Message-ID: 1032454783.21664.308.camel@linda (view raw or flat)
Thread:
Lists: pgsql-novice
On Thu, 2002-09-19 at 15:14, Rory Campbell-Lange wrote:
> I have problems after exporting then importing data into newly created
> databases that the sequence and the indexed serial fields get out of
> alignment.
> 
> I wonder if it isn't better to always simply insert ids using some sort
> of function or other procedure to calculate on each insert a new unique
> id number.

After you import data, you must also reset the sequence:

  SELECT setval('sequence_name',
        (SELECT MAX(sequence_column) FROM table));
-- 
Oliver Elphick                                Oliver(dot)Elphick(at)lfix(dot)co(dot)uk
Isle of Wight, UK                            
http://www.lfix.co.uk/oliver
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839  932A 614D 4C34 3E1D 0C1C
                 ========================================
     "Bring ye all the tithes into the storehouse, that 
      there may be meat in mine house, and prove me now 
      herewith, saith the LORD of hosts, if I will not open 
      you the windows of heaven, and pour you out a  
      blessing, that there shall not be room enough to  
      receive it."           Malachi 3:10 


In response to

Responses

pgsql-novice by date

Next:From: Rory Campbell-LangeDate: 2002-09-19 17:09:35
Subject: Re: Make an id field max(id)+1 rather than SERIAL
Previous:From: Rory Campbell-LangeDate: 2002-09-19 14:14:17
Subject: Make an id field max(id)+1 rather than SERIAL

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group