#1 2016-07-27 06:47:58

oraclei
Member
Registered: 2016-06-06
Posts: 18

Why Client.Add can not save the associated instance's correct Id ?

I define two class as follow:

    TRoute = class(TSQLRecord)
    private
        fRouteNumber: string;
        ...
    published
        property routeNumber: string read fRouteNumber write fRouteNumber;
        ...
    end;

    TSchedule = class(TSQLRecord)
    private
        fRoute: TRoute;
        ...
    published
        property route: TRoute read fRoute write fRoute;
        ...
    end;

TSchedule include TRoute's ID, now I want to save a TSchedule instance, and it's routeId is correct:   

procedure TMyForm.btnOKClick(Sender: TObject);
var
    schedule: TSchedule;
    Id: Integer;
begin    
    schedule := TSchedule.Create(Client, StrToInt(txtId.Text));
    try
        schedule.route := TRoute.Create(Client, fRouteId);
        ...
       Id := Client.Add(schedule, true);
    finally
       schedule.Free;
    end;
end;    

When the Client.Add execute, the database table has the route id 24670096, not the correct value 2, what wrong with me?

Last edited by oraclei (2016-07-27 06:48:56)

Offline

#2 2016-07-27 07:05:02

edwinsn
Member
Registered: 2010-07-02
Posts: 1,217

Re: Why Client.Add can not save the associated instance's correct Id ?

It might not obvious, but the third parameter called "ForceId" must be "True", in order to save to DB using the ID you assigned.

Last edited by edwinsn (2016-07-27 07:05:28)


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#3 2016-07-27 07:16:38

oraclei
Member
Registered: 2016-06-06
Posts: 18

Re: Why Client.Add can not save the associated instance's correct Id ?

I use Client.Add(schedule,true, true); but the route id still incorrect. and I could not find the TSQLHttpClientWinHTTP.Add API doucment online or in synopse's PDF.

Offline

#4 2016-07-27 07:21:53

oraclei
Member
Registered: 2016-06-06
Posts: 18

Re: Why Client.Add can not save the associated instance's correct Id ?

In Delphi debug state, I can find out:
schedule -> fRoute -> TSQLRecord -> fID = 2

And the Id = 2 TRoute record actually in table bt_route.

Offline

#5 2016-07-27 07:40:53

oraclei
Member
Registered: 2016-06-06
Posts: 18

Re: Why Client.Add can not save the associated instance's correct Id ?

@edwinsn

I find out the docs: TSQLRest.Add , but the ForceID is not for associated instance,but the main instance, as the doc said:
if ForceID is true, client sends the Value.ID field to use this ID for adding the record (instead of a database-generated ID)

Offline

#6 2016-07-27 08:24:50

oraclei
Member
Registered: 2016-06-06
Posts: 18

Re: Why Client.Add can not save the associated instance's correct Id ?

I found the result in
https://tamingthemormot.wordpress.com/2 … tionships/

thanks Taming the mORMot!

Offline

Board footer

Powered by FluxBB