Getting information about foreign keys

Discussion of open issues, suggestions and bugs regarding SDAC (SQL Server Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
jaska45
Posts: 28
Joined: Fri 23 Dec 2005 03:55

Getting information about foreign keys

Post by jaska45 » Tue 08 May 2007 00:56

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

Jackson
Posts: 512
Joined: Thu 26 Jan 2006 10:06

Post by Jackson » Tue 08 May 2007 08:07

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.

jaska45
Posts: 28
Joined: Fri 23 Dec 2005 03:55

Post by jaska45 » Tue 08 May 2007 23:37

Thank you. I am waiting for the new build :D

Jaakko

jaska45
Posts: 28
Joined: Fri 23 Dec 2005 03:55

Post by jaska45 » Wed 09 May 2007 11:00

Is this feature fixed in version 4.0?

Best regards,
Jaakko

Jackson
Posts: 512
Joined: Thu 26 Jan 2006 10:06

Post by Jackson » Thu 10 May 2007 12:37

This fix also will be included in the next build of SDAC 4.

Post Reply