Page 1 of 1

How to Handle AutoIncs (or SERIALS in Postgres)

Posted: 30.08.2008, 01:16
by pawelp
Halo,

I'm using TClientDataset, TDatasetProvider and TZQuery.
I have a postgreSQL database with a simple table with a field called id of type SERIAl and a field 'name' of type varchar(20).

I fould an article "How to use AutoInc fields with DataSnap" (http://dn.codegear.com/kr/article/20847) which describes how to handle the autoinc fields correctly. In brief, you should assign a negative value (eg. -1) to the "id" Field, call TClientDataset.ApplyChanges, and then TClientDataset.refresh to get the correct id value from the database.

I'm not sure if it is zeos or postgres as such, but the "id" fields are not excluded from the update statment, and they are stored in the table as negative values, so when I call TClientDataset.refresh, I get my id=-1 back from the database...

One thing which I noticed is, that the "id" field which is of type SERIAL seems not to be handled as an autoinc field. The property TClientDataset.FieldByName('id').Datetype indicates that it is an integer value not an autoinc.

Any Ideas how to manage this?

ps. its quite late now... so, if it is not clear what I mean, please excuse me, just tell me and I will try to describe it better :)

Posted: 31.08.2008, 16:53
by btrewern
See the ZSequence component. It's made specifically for this.

Regards,

Ben

Posted: 18.12.2011, 15:55
by Ðerek wildstar
How to use this component? Seem self-explanatory but I do not want to manage sequences on the application. The sequences are automatically executed on the DATABASE.

So, the TZSequencev does what?

1. Exposes the PG Sequence to the Zeos componentes use it?

or

2. Executes the PG Sequence, get the result and assign to a field (the same field that would be automatically filled by the Database)

The second approach is not good. So, i need to know

Thanks in advance

Posted: 18.12.2011, 16:19
by Ðerek wildstar
I've done a test with the TZSequence. Using it, my autoinc field (bigserial) is increased 2 times. So, i guess, this component can only be used on fields with no sequence attached, with the intent of apply a autoinc on ANY field but the true Database (PG) Autoinc field