Page 1 of 1

Getting information about foreign keys

Posted: Tue 08 May 2007 00:56
by jaska45
Hi,

I try to get information about the foreign keys of the table.
I use Delphi 7 and SQL Server 9.00.3042.00

I have (ignore this SQL syntax. I won't work on SQL Server but is much easier to read in this email than dump from SQL server)

CREATE TABLE Resource
(
Id INTEGER NOT NULL,
Comment VARCHAR(50),
PRIMARY KEY(Id)
);

CREATE TABLE Country
(
ThisId INTEGER NOT NULL,
Id INTEGER NOT NULL,
Language VARCHAR(10) NOT NULL,
Name VARCHAR(50) NOT NULL,
Population INTEGER NOT NULL,
Capital VARCHAR(50) NOT NULL,
Description VARCHAR(200) NOT NULL,
PRIMARY KEY(ThisId),
FOREIGN KEY(Id) REFERENCES Resource(Id)
);

and I use code

procedure TLaSqlServerDatabase.DoGetForeignKeys(dataSet: TDataSet; keys: TTntStrings);
var
field: TStringField;
metadata: TMsMetadata;
begin
metadata := TMsMetadata.Create(nil);
try
metadata.Connection := FConnection;
metadata.DatabaseName := metadata.Connection.Database;
metadata.TableName := (dataSet as TMsTable).TableName;
metadata.ObjectType := otForeignKeys;
metadata.ObjectType := otColumns;
metadata.Open;

while not metadata.Eof do
begin
field := metadata.FieldByName('FK_COLUMN_NAME') as TStringField;
keys.Add(field.Value);

metadata.Next;
end;
finally
metadata.Free;
end;
end;

The problem is that it show the foreign key in Resource table but not in Coutry table (where it is actually defined). I mean when metadata.TableName = 'Resource' I get foreign key items but when it is 'Country' the data set is empty.

How can I enumerate foreign keys of Country table?

Best regards,
Jaakko

Posted: Tue 08 May 2007 08:07
by Jackson
Thank you for information.
We have reproduced the problem and fixed it.
This fix will be included in the next build of SDAC 3.
Please watch for announcements at the forum.

Posted: Tue 08 May 2007 23:37
by jaska45
Thank you. I am waiting for the new build :D

Jaakko

Posted: Wed 09 May 2007 11:00
by jaska45
Is this feature fixed in version 4.0?

Best regards,
Jaakko

Posted: Thu 10 May 2007 12:37
by Jackson
This fix also will be included in the next build of SDAC 4.