Page 1 of 1

TZQuery & LAST_INSERT_ID()

Posted: 22.09.2005, 13:00
by hetfield
Hi,

i am new to ZEOS components.

I use TZQuery with TDatasetProvider & TClientDataset to access a mysql-server 4.1.15 from BCB 6!

My test app is a simple master-detail-relation.

How can i get the autogenerated id after inserting the master record.

A simple "select LAST_INSERT_ID()" in TZQuery always return 0!

BDE's TQuery return the correct id of the master record.

I need the correct value to correct the corresponding master id in the detail record!

How can i get the right value?

Thx

hetfield

Posted: 23.09.2005, 06:03
by fduenas
Plz post always teh zeos version (i recommend the last one from CVS), delphi version also.

Are you sure you're posting the changes to the server?, make sure CachedUpdates is set to false.

I always use this method and don't have problems.

Just a TIP. When a master record is append and then posted, tzquery will refresh the content of the detail table, so you must:
1) unlink the detail table oin the OnBeforePost event of master table (Set detail's MasterdataSource property to nil),
2) get the last insert id,
3) replace the id key field in detail table from the first record to the last,
4) and in the OnAfterPostEvent of Master table link again the detail table with the master table.

I normally don't use master-detail relationship, but it needs that all records must be loaded in the Detail Dataset, I normally use the 'DataSource' property of TZQuery to link it to another table, normally this method is called Parametrized Queries.