#1 2011-10-28 08:24:11

corchi72
Member
Registered: 2010-12-10
Posts: 232

How do you declare a foreign key?

Hello, I have a question to ask yourself, I have a class that has TSQLQVW a property called "connection" that refers to another class TSQLConnection, what is the right way to write the class TSQLQVW not have to go around I have written below.
Is there another solution?
I did not want to use the usual class "TSQLRecordMany" to create the relationship because it is a type of relationship one to many but just a one to one

  TSQLQvwConnections= class(TSQLRecordMany)
  private
    fSource: TSQLQvw;
    fDest: TSQLConnection;
  published
    property Source:  TSQLQvw read fSource;
    property Dest: TSQLConnection read fDest;
  end;

This is the code that I implemented and it works but I think it's the right solution

 
  TSQLConnection = class(TSQLFile)
  private
    fOwner: RawUTF8;
    fEnabled: boolean;
    fConnectionString:RawUTF8;
 ...  
  published

    property Owner: RawUTF8 read fOwner write fOwner;
    property Enabled: boolean read fEnabled write fEnabled;
    property ConnectionString:RawUTF8 read fConnectionString write fConnectionString;
  end;
  
  
  
  TSQLQvw = class(TSQLFile)
  private
    fOwner: RawUTF8;
    fEnabled: boolean;
  ...
    fConnection: TSQLConnection;
    FConnectionID: Integer;
    procedure SetConnectionID(const Value: Integer);
    function GetConnection: TSQLConnection;
    procedure SetConnection(const Value: TSQLConnection);
    property Connection: TSQLConnection read GetConnection write SetConnection;
   published

    property Owner: RawUTF8 read fOwner write fOwner;
    property Enabled: boolean read fEnabled write fEnabled;
   ...
    property ConnectionID:Integer read FConnectionID write SetConnectionID;
  end; 
  
  var
  Model: TSQLModel;
  currentClient : TSQLRestClientUri;
  
  ....
  
 implementation 
  
  
procedure TSQLQvw.SetConnection(const Value: TSQLConnection);
begin
  FConnectionID := Value.ID;
end;

procedure TSQLQvw.SetConnectionID(const Value: Integer);
begin
  FConnectionID := Value;
end;


function TSQLQvw.GetConnection: TSQLConnection;
begin
  result := TSQLConnection.Create(currentClient,FConnectionID);
end; 

Offline

#2 2012-01-23 09:39:44

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,667
Website

Re: How do you declare a foreign key?

You should better use directly TSQLConnection without any Get/SetConnection, then typecast into a PtrInt.
This is a bit tricky, but this is how the framework expects it to be defined.

See the documentation about "one to one" relationship.
There will be a dependency check made automatically by the framework.

Online

Board footer

Powered by FluxBB