Subject Re: [IBO] Trigger to Autoincrement Doesn't Fire
Author Helen Borrie
At 12:24 p.m. 15/01/2013, you wrote:
>I have a FB2.5 table with a generator and trigger to increment a PK. In IBExpert, the table increments correctly when I add a record. However, when I try to do it using code in Delphi XE with IBObjects (TIBOTable). I get an error that says the primary key field must have a value.
>
>This is the code I use:
>
>tableGIS_Config.Open;
>tableGIS_Config.Insert;
>tableGIS_ConfigUSER.Value := User;
>tableGIS_Config.Post;

Autoincrement by trigger happens in the database, not in the client. So if you do an insert (which is a client-side action) IBO has no way to assign a value to the primary key field (which is obligatory) and so you get that error.

IBO provides GeneratorLinks, which you can set at either session or dataset level, so that the client side will fetch the generator value in any BeforeInsert event, in time to avoid going into insert mode with no value. Look it up. :-)

Incidentally, it's vital to write your autoincrement triggers so that they fire only if an application passes *no* value for it (the field is excluded from the insert set), viz.,

IF (MY_NUM IS NULL) THEN
NEW.MY_NUM = GEN_ID (MY_NUM_GENERATOR, 1);

Helen




Helen