Page 1 of 1

TZTable tries to set Identity column on insert

Posted: 12.04.2017, 06:50
by danielsap
Hi,
I created simple application in Delphi - TZConnection, TZTable, TDataSource, TDBGrid.
Also created a table in FireBird with identity column
create table Perspective (
Id integer generated by default as identity primary key,
Name varchar(64)
);

When I run the application and type value for Name in the DBGrid,
and press down arrow button to insert the record, it throws an exception:
'Project ZeosTest raised exception class EZSQLException with message'SQL Error: validation error for column "PERSPECTIVE"."ID" value "***null***".
Error code - 625. Insert failed because a column definition includes validation constraints.
The SQL: INSERT INTO PERSPECTIVE (ID, NAME) VALUES (?, ?);'.

I tried to solve the problem by:
1. adding all fields in the TZTable
2. setting property 'AutoGenerateValue' to arAutoInc in the TZTable
3. setting property 'Required' to False in the TZTable

This application is very basic and a lot of people starts using components and database this way.
Also using simple table is used very often.
So, I expected this to work out of the box, just by connecting the components and to go smooth.

Can you give me some more information and may be some solution to this issue?

I'm using
Zeos 7.2.1-rc
Firebird-3.0.2.32703-0_Win32
Delphi XE3

Thanks
Daniel

Re: TZTable tries to set Identity column on insert

Posted: 12.04.2017, 18:29
by marsupilami
Hello Daniel,

the answer is simple: Don't use autoinc because it is a mess with GUI-Applications. You better do it like this:
  • Create a Sequence (Generator in Firebird-Speak)
  • make your field to be just integer ot bigint
  • Add a TZSequence to your form, set the SequenceName property to the name of your newly created seqence
  • set the Sequence property of your TZTable object to the newly created TZSequence object
  • set the SequenceField property to your primary key field, where you want to insert the sequence value
Zeos will now automatically retrieve sequence values for you and store them in the field.

Internally Firebird is doing exatly this - it creates a before insert trigger and retrieves a new value from an automatically created sequence if the autoinc field is null. But only if you do it this way, Zeos can get all the information, it needs.

With best regards,

Jan

Re: TZTable tries to set Identity column on insert

Posted: 13.04.2017, 05:38
by danielsap
Thank you, Jan
It works fine this way (y)