#1 2018-07-02 11:21:47

dualarrow
Member
From: Australia
Registered: 2018-06-28
Posts: 21

Is this really the correct way to add a record with a foreign key

I'm trying to get some simple ORM to work. My TSQLRecord structures look like this

 TWorkstation = Class(TSQLRecord)
  private
    FName: String;
  Published
    Property Name: String Read FName Write FName;
  End;

  TService = Class(TSQLRecord)
  private
    FName: String;
    FWorkstation: TWorkstation;
  Published
    Property Name: String Read FName Write FName;
    Property Workstation: TWorkstation Read FWorkstation Write FWorkstation;
  End;

Where TService has a pointer to a TWorkstation.

With the following code

Var
  x: TWorkstation;
  y: TService;
Begin
  x := TWorkstation.Create;
  x.Name := 'WS1';
  FServer.Add(x,True);

  y := TService.Create;
  y.Name := 'Svc-1';

  y.Workstation := x;
  FServer.Add(y, True);
End

Ignoring the leaks, the end result is 2 sqlite tables with the correct fields defined, but the Workstation table has a huge number for the service entry. If instead, before the 2nd add I perform

  y.Workstation := Pointer(x.ID);

It gets the correct value of 1 in it. Is this the wrong way to go about adds like this or do I need to perform this odd operation before any writes of tables with pointers to other tables ?

Offline

#2 2018-07-02 20:37:37

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

Re: Is this really the correct way to add a record with a foreign key

Yes, please check the documentation about it.

You may also write

y.Workstation := x.AsTSQLRecord;

Offline

Board footer

Powered by FluxBB