Page 1 of 1

How to read primary key

Posted: Mon 07 May 2007 07:30
by jaska45
I am trying to read the primary key info using TMyTable.IndexDefs

However IndexDefs.Options does not contain ixPrimary even if the index is a primary key.

How to read primary key info?

Best regards,
Jaakko

Posted: Mon 07 May 2007 08:52
by Antaeus
Please specify some addition information:
- Pascal code you use to get the information about key fields;
- script to create your table;
- exact version of Delphi, C++Builder or Kylix;
- exact version of MyDAC. You can see it in the About sheet of TMyConnection Editor;
- exact version of MySQL server and MySQL client. You can see it in the Info sheet of TMyConnection Editor.

Posted: Mon 07 May 2007 09:05
by jaska45
MySQL version: 5.0.24a
MySQL client: 5.0.11

MyDAC verison: I don't know because I use them from code only. Have not installed them because installing messes up my Delphi. I do not have source code but DCU only. Date is
22.10.2005 00:23 103 685 MemData.dcu

I am using Delphi 7

Here is code:

procedure TLaMySqlDatabase.DoGetForeignKeys(dataSet: TDataSet; keys: TTntStrings);
begin
IndexDefsToForeignKeys((dataSet as TMyTable).IndexDefs, keys);
end;

procedure TLaDatabase.IndexDefsToForeignKeys(value: TIndexDefs; keys: TTntStrings);
var
i: Integer;
begin
for i := 0 to value.Count - 1 do
if not (ixPrimary in value.Options) then
keys.Add(value.Fields);
end;

In the above Options does not contains ixPrimary

I used MySQL Administrator to create the table not SQL.
I tried to attach a screnshot to this forum but could not. How to do that?

Best regards,
Jaakko

Posted: Mon 07 May 2007 09:11
by jaska45
Here is SQL code

DROP TABLE IF EXISTS `original`.`original`;
CREATE TABLE `original`.`original` (
`Id` int(10) unsigned NOT NULL auto_increment,
`Original` int(10) unsigned NOT NULL,
`Language` varchar(10) NOT NULL,
`Name` varchar(45) NOT NULL,
PRIMARY KEY (`Id`),
KEY `FK_Original_1` (`Original`),
CONSTRAINT `FK_Original_1` FOREIGN KEY (`Original`) REFERENCES `original` (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

You see that Id has primary key bit TMyTable.IndexDefs[0].Options does not have it.

Jaakko

Posted: Mon 07 May 2007 09:16
by Antaeus
jaska45 wrote: MyDAC verison: I don't know because I use them from code only. Have not installed them because installing messes up my Delphi. I do not have source code but DCU only. Date is
22.10.2005 00:23 103 685 MemData.dcu
Check the MydacVersion constant at run time (the MyAccess unit should be included into the uses clause).

Posted: Mon 07 May 2007 09:25
by jaska45
I installed the most recent version and the same behaviour exisit.

'4.40.0.24'

Jaakko

Posted: Mon 07 May 2007 12:29
by Antaeus
Thank you for information.
This property does not provide the information about primary keys. Try to use the following way:

Code: Select all

  for i := 0 to MyTable.Fields.Count - 1 do
    if pfInKey in MyTable.Fields[i].ProviderFlags then
      // ...
Another way to request such meta information is to execute a MySQL command like 'SHOW COLUMNS FROM tb_name';

Posted: Mon 07 May 2007 13:02
by jaska45
How is this doing with primary key. I don't understand. According to VCL's documentation is NOTHING to do with primary keys. Can tell little bit more about your idea.

On avery other data control (e.g. TTable, TIBTable, TAdoTable) IndexDefs works. Why is it so that your controls do not work in the same was as VCL's own controls. Even your different controls (e.g. MyDAC, ODAC and SDAC) they all work in the different way. It is very hard to use them because they do not work in the same way.

Jaakko

Posted: Tue 08 May 2007 11:39
by Antaeus
Provider flags are filled by TMyTable for the reasons of compatibility with Midas (see Delphi help for more information). You can use these flags to obtain the information you need.

I tested this issue with BDE, ADO and MySQL server. They also do not allow determining primary keys in this way. Please send me an example at evgeniyd*crlab*com if I am wrong.
We well think about filling the ixPrimary option of IndexDef in one of the next MyDAC builds.
We are investigating the possibility to add to MyDAC a new component for retrieving metainformation like TMSMetadata in SDAC.