Re: Make an id field max(id)+1 rather than SERIAL - Mailing list pgsql-novice

From Oliver Elphick
Subject Re: Make an id field max(id)+1 rather than SERIAL
Date
Msg-id 1032454783.21664.308.camel@linda
Whole thread Raw
In response to Make an id field max(id)+1 rather than SERIAL  (Rory Campbell-Lange <rory@campbell-lange.net>)
Responses Re: Make an id field max(id)+1 rather than SERIAL
List 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.Elphick@lfix.co.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


pgsql-novice by date:

Previous
From: Rory Campbell-Lange
Date:
Subject: Make an id field max(id)+1 rather than SERIAL
Next
From: Rory Campbell-Lange
Date:
Subject: Re: Make an id field max(id)+1 rather than SERIAL