Recent

Author Topic: Firebird Auto_Increment [SOLVED]  (Read 10709 times)

lennit

  • Jr. Member
  • **
  • Posts: 63
  • Aquila non capit muscas.
Firebird Auto_Increment [SOLVED]
« on: July 22, 2014, 08:07:28 pm »
Hello,

I've got an issue with ID autoincrement generator. I searched the forum and found this:
Code: [Select]
IBConnection1.ExecuteDirect('CREATE GENERATOR GEN_FIELDS_ID;');
  IBConnection1.ExecuteDirect('SET GENERATOR GEN_FIELDS_ID TO 0;');

  IBConnection1.ExecuteDirect('CREATE TRIGGER TR_FLD FOR FIELDS ACTIVE BEFORE INSERT POSITION 0 AS '+
                              'BEGIN if (NEW.ID is NULL) then NEW.ID = GEN_ID(GEN_FIELDS_ID, 1);END;');

I also put beforehand triggers in my database (using IBEasy+) following this example:

Code: [Select]
CREATE TRIGGER trgTTEST_BI_V3 for TTEST
active before insert position 0
as
begin
  if ((new.id is null) or (new.id = 0)) then
  begin
    new.id = gen_id( gidTest, 1 );
  end
end

The problem is somehow still the ID is not generated when i use the DBNavigator. I get:

SQLQuery1: Field ID is required but not supplied.

What am I doing wrong?
« Last Edit: July 23, 2014, 03:56:14 pm by lennit »
Win 7 32/64
Win 10 64bit
Lazarus 1.4.4
Firebird 2.5.3 (x64)

korba812

  • Sr. Member
  • ****
  • Posts: 394
Re: Firebird Auto_Increment
« Reply #1 on: July 22, 2014, 08:33:14 pm »
Set 'Required' property to false.

eg.:
SQLQuery1.FieldByName('ID').Required := False;

lennit

  • Jr. Member
  • **
  • Posts: 63
  • Aquila non capit muscas.
Re: Firebird Auto_Increment
« Reply #2 on: July 22, 2014, 08:40:46 pm »
That solved my problem with the error code  :D However the database is not updated in the end. I wonder already if to give up on Navigator and make my own buttons XD
Win 7 32/64
Win 10 64bit
Lazarus 1.4.4
Firebird 2.5.3 (x64)

exdatis

  • Hero Member
  • *****
  • Posts: 668
    • exdatis
Re: Firebird Auto_Increment
« Reply #3 on: July 22, 2014, 09:16:48 pm »
You can use a simple view to find next_id, eg:
create view vmy_table_id(next_id) as
select
  gen_id(your_generator, 1)
from
  rdb$database;

Now you can use beforePost event to set id:
eg.
if(my_tablemy_id.IsNull) then //new record
  //select query vmy_table_id

also:
http://www.firebirdsql.org/refdocs/langrefupd21-ddl-sequence.html

create view...
select
  next value for sequence_name
from
  rdb$database
« Last Edit: July 22, 2014, 09:32:40 pm by exdatis »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Firebird Auto_Increment
« Reply #4 on: July 22, 2014, 09:26:49 pm »
That solved my problem with the error code  :D However the database is not updated in the end. I wonder already if to give up on Navigator and make my own buttons XD

If I remember correctly the check
Code: [Select]
if (NEW.ID is NULL) then will be true only when the insert statement is something like
Code: [Select]
insert into fields (Id,......) values (null,.....) if for example the insert statement does not include the ID field at all then it will always be false at least that is what I remember from a few years when I had the same problem I think that that problem was addressed in newer version of the firebird database but I do not remember how it was addressed or at which version so better ask on a firebird list for more information on that error.

PS. What the navigator has to do with the server?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

lennit

  • Jr. Member
  • **
  • Posts: 63
  • Aquila non capit muscas.
Re: Firebird Auto_Increment
« Reply #5 on: July 23, 2014, 09:56:46 am »
This is a sample database with a sample project. My problem with Navigator is that i don't control the information sent to the database, so when i try to post a new record all i get is a record with null index i can reach in runtime and no information sent to the database.
There must be some major thing i miss here, because i am sure this same component works fine for everybody. I use Firebird 2.5.3 (64bit)
« Last Edit: July 23, 2014, 11:11:21 am by lennit »
Win 7 32/64
Win 10 64bit
Lazarus 1.4.4
Firebird 2.5.3 (x64)

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: Firebird Auto_Increment
« Reply #6 on: July 23, 2014, 01:38:57 pm »
My problem with Navigator is that i don't control the information sent to the database, so when i try to post a new record all i get is a record with null index i can reach in runtime and no information sent to the database.
Taking a (very quick) look at your program i'm missing two things.

The first is i'm missing the ApplyUpdates. Even when you do a post (with the navigator) you need to do an Applyupdates on the changed dataset to transfer the data to the database.

Second is the missing commit on the transaction. You have a default transaction with caRollback, which means your changes would be rolled back when you don't commit the transaction explicitly.

So you can do the following (with a button, at the end of your program or after every post):

Code: [Select]
  mainQ.ApplyUpdates;
  if IBC.Transaction.Active then
    IBC.Transaction.CommitRetaining;

@taazz: No, it is not needed to have the ID in the statement. NEW.ID will still be NULL if it has not been defined. You can do an insert with just one field value and the trigger should still assign an increment number to ID. (It has been so since at least fb 1.5. I'm not sure if you're experience is with an even older version :))
« Last Edit: July 23, 2014, 01:46:24 pm by rvk »

lennit

  • Jr. Member
  • **
  • Posts: 63
  • Aquila non capit muscas.
Re: Firebird Auto_Increment
« Reply #7 on: July 23, 2014, 03:55:55 pm »
Thank you all for the replies! I learned a lot and actually made it work  :D. Especially to rvk: thank you :)
Win 7 32/64
Win 10 64bit
Lazarus 1.4.4
Firebird 2.5.3 (x64)

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Firebird Auto_Increment
« Reply #8 on: July 23, 2014, 03:59:54 pm »
@taazz: No, it is not needed to have the ID in the statement. NEW.ID will still be NULL if it has not been defined. You can do an insert with just one field value and the trigger should still assign an increment number to ID. (It has been so since at least fb 1.5. I'm not sure if you're experience is with an even older version :) )

My experience starts with the first interbase 6.x public releases and from there to firebird 1.0 and later versions. So it might be that old yes I do not keep a time table of bugs.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

 

TinyPortal © 2005-2018