Subject Re: [firebird-support] Problem in INSERT query
Author Lucas Franzen
Adriano schrieb:
> Hi,
> Firebird 2 RC1 Windows, Driver ODBC, Visual Basic
> I've a table FATTURE with some fields,
> a field named IDFATTURA which is Primary Key (not null, unique and active)
> a autoincremental trigger in Before Insert
> BEGIN
> IF (NEW.IDFATTURA IS NULL) THEN
> NEW.IDFATTURA = GEN_ID(GEN_FATTURE_IDFATTURA, 1);
> END
>
> If i execute a simple query:
> query = "INSERT
> cn.execute query
>
> I receive this error message:
> Invalid insert or update value(s): object columns are
> constrained - no 2 table rows can have duplicate column values.
> Violation of PRIMARY or UNIQUE KEY constraint "PRIMARYKEY012" on table
> "FATTURE".

It looks like your generator GEN_FATTURE_IDFATTURA gives you back a new
value which is always assigned to an existing record.

"Autoincremental" by generators is not safe if you do also insert
records by other means.

For example:
If at any place in your program (or elsewhere) you do an
INSERT INTO FATTURE (IDFATTURA, Oggetto) VALUES (15, 'test')
/* note: use SINGLE quotes for values, not double ones*/
this will be okay (as long as there's no record with IDFATTURA = 15 so far).

But if your generator GEN_FATTURE_IDFATTURA is at value 14 at the moment
and you do an:
INSERT INTO FATTURE (Oggetto) VALUES ('test') the trigger will supply a
new value for IDFATTURA of 15 which will lead to the error you described.

Luc.