Обсуждение: Re: Migration problem - serial fields

Поиск
Список
Период
Сортировка

Re: Migration problem - serial fields

От
missive@frontiernet.net (Lee Harr)
Дата:
> The snag I'm encountering is with Identity fields in SQL7.  I
> discovered the equivalent in PG -- sequences/serial field.  I would
> like to use serial if possible, but here's the problem:  getting the
> existing records over while *keeping the existing values* for the
> Identity/sequence ID fields.  They actually transfer over fine, but
> the next insert into the  PG table generates a duplicate ID error.
> Obviously my transfer did not update the sequence used by the serial
> field -- it's still stuck at 1.  However, I'm just not sure of the
> best course of action.
>


SELECT setval('your_seq_name', 654); -- set higher than highest record.


Re: Migration problem - serial fields

От
Jeff Boes
Дата:
On Mon, 04 Mar 2002 19:57:50 -0500, Lee Harr wrote:

> SELECT setval('your_seq_name', 654); -- set higher than highest record.


Even better:

SELECT setval('your_seq_name',max(your_seq_col)) from your_table;

You can get away with max, rather than max+1, because the next call to
the sequence will generate max() plus the interval (usually 1).

--
Jeff Boes                                             vox 616.226.9550
Database Engineer                                     fax 616.349.9076
Nexcerpt, Inc.                                      jboes@nexcerpt.com