Page 1 of 1

How to identify primary key field(s)

Posted: 14.11.2017, 23:15
by ertank
Hello,

I am using ZeosLib with Lazarus 1.8.0RC5, Raspberry Pi 3 running Stretch OS, PostgreSQ 9.6.5 database.

Given that I have table name, can I find primary key field (or fields) of that table using zeoslib?

If yes, I appreciate a small sample code, please.

Thanks
Ertan

Re: How to identify primary key field(s)

Posted: 14.11.2017, 23:31
by ertank
Seems like it is not quite possible to find it in an executed TZQuery, but it is possible to use a query for that matter. Below link seems to provide what is necessary
https://wiki.postgresql.org/wiki/Retrie ... ey_columns

Re: How to identify primary key field(s)

Posted: 15.11.2017, 07:52
by Fr0sT
TZPostgreSQLDatabaseMetadata.UncachedGetPrimaryKeys ?

Re: How to identify primary key field(s)

Posted: 17.11.2017, 22:46
by ertank
Fr0sT wrote:TZPostgreSQLDatabaseMetadata.UncachedGetPrimaryKeys ?
Hello Fr0sT,

Thanks for your help.

Unfortunately, I am not sure how I can use that function as it seems to be a protected function and used ZeosLib internals as far as I could understand.

If it is to strip SQL out of that function, I just lost myself in it doing that. However, I confirm my above SQL is working OK for PostgreSQL 9.6.4

Regards,
Ertan

Re: How to identify primary key field(s)

Posted: 18.11.2017, 11:26
by marsupilami
Hello Ertan,

(Uncached)GetPrimaryKeys is part of the DBC layer. It might be more easy to use the TZMetadata component. It works like a query. Set the metadata you want to retrive to primary key. Set the table name to your desired table name. Call the open method.

With best regards,

Jan