How to Handle AutoIncs (or SERIALS in Postgres)

In this forum we will discuss things relating the ZEOSLib 6.6.x stable versions

Moderators: gto, EgonHugeist

Post Reply
pawelp
Fresh Boarder
Fresh Boarder
Posts: 16
Joined: 18.08.2008, 18:10
Location: Poland

How to Handle AutoIncs (or SERIALS in Postgres)

Post 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 :)
btrewern
Expert Boarder
Expert Boarder
Posts: 193
Joined: 06.10.2005, 18:51

Post by btrewern »

See the ZSequence component. It's made specifically for this.

Regards,

Ben
Ðerek wildstar
Junior Boarder
Junior Boarder
Posts: 35
Joined: 30.08.2005, 16:30
Location: Olinda / PE
Contact:

Post 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
Ðerek wildstar
Junior Boarder
Junior Boarder
Posts: 35
Joined: 30.08.2005, 16:30
Location: Olinda / PE
Contact:

Post 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
Post Reply