Subject Re: Auto-increment and generators
Author h_urlaf
--- In firebird-support@yahoogroups.com, Milan Babuskov <albis@e...>
wrote:

> If you're setting it to some specific ID which is > gen_id(g1, 0),
> then the best thing is to SET GENERATOR g1 TO that_id + 1;

but that would still fail

INSERT INTO TEST (ID) VALUES (2); SET GENERATOR g1 TO 3;
INSERT INTO TEST (ID) VALUES (1); SET GENERATOR g1 TO 2;
INSERT INTO TEST (DATA) VALUES (NULL); /* oops */

maybe an AFTER INSERT OR UPDATE trigger could set the generator to
MAX(ID) if new.ID was not NULL?

> > Then what is the point of things like
> >
> > if (new.ID is null) then new.ID = gen_id(g1, 1);
>
> The point is that sometimes you want to copy data from other table,
> or import data from some external source, and you want to be able to
> set the proper values for primary key (since it may be referenced
> from other table)

Very good point, which underscores that my question wasn't just
hypothetical. After the import, the generator would be 'out of sync',
and I could get PK conflicts even with the generator.

Emiliano