Association references a transient object.on ApplyUpdates

Hello!

I have a problem when applying updates in a dataset. This method worked until few months ago, but after opening a project, recompiling it and running it it fails when i do an ApplyUpdates. The error is in the XData server after calling ApplyChanges in the WebCore app.

Project Orders.Server.VCL.exe raised exception class EAssociationReferencesTransientObject with message 'Association references a transient object. 
Parent: Entities.TOdvoz(12), Transient: Entities.TVehicle(5), Association: FVehicle'.

The code below is simplified, to be shorter I removed all the other properties that are not related.

First I save the vehicle object in a TWebDataModule property

..
TmodMain = class(TWebDataModule)
private
  ...
public
  ...
  Vozilo: TObject;
 ...
end;

When the user logs in the WebCore application, I save the vehicle object for later usage:

procedure TmodMain.wdsVehicleAfterOpen(DataSet: TDataSet);
begin
    Vehicle := TObject(TXDataWebDataSet(DataSet).CurrentData);
end;

Then (in another form of the WebCore application) I use the object after opening a dataset.

procedure TfrmOdvoz.wdsOdvozAfterOpen(DataSet: TDataSet);
var cas: TTime;
begin
    wdsOdvoz.Append;
    wdsOdvoz.FieldByName('Id').AsInteger := 0; // New record, without this I get an error on Posting.
    wdsOdvoz.FieldByName('Vehicle').Value := modMain.Vehicle;
    // Tried also this, but same result.
//    wdsOdvoz.FieldByName('Vozilo').Value := modMain.wdsVozila.CurrentData;

    wdsOdvoz.Post;
end;

When i click the save button, this code is executed

 .. editing of the wdsOdvoz fields, not related to field "Vehicle"
  wdsOdvoz.ApplyUpdates;

The error is raised on the REST server (XData). It all worked without problems a few months before.

Please, help!

Kind regards
m@rko

  [Entity]
  [Table('Odvoz')]
  [Id('FId', TIdGenerator.IdentityOrSequence)]
  [EntityAuthorize]
  TOdvoz = class
  private
    [Column('Id', [TColumnProp.Required, TColumnProp.NoInsert, TColumnProp.NoUpdate])]
    FId: Integer;
        
    [Association([TAssociationProp.Lazy, TAssociationProp.Required], [])]
    [JoinColumn('Vozilo', [TColumnProp.Required], 'Id')]
    FVehicle: Proxy<TVozilo>;

I also tried to use this

TJSObject(wdsOdvozPodrobno.CurrentData)['Vehicle@xdata.ref'] := Format('Vehicle(%d)', [modMain.VehicleId]);

but this raises a different error

Odvoz.pas:208  Uncaught TypeError: Cannot set properties of undefined (setting 'Vehicle@xdata.ref')

So no improvement here.. :frowning:

The problem is in the mapping of FVehicle association, it's missing the cascades. Use this:

  [Association([TAssociationProp.Lazy, TAssociationProp.Required], CascadeTypeAllButRemove)]
  [JoinColumn('Vozilo', [TColumnProp.Required], 'Id')]
  FVehicle: Proxy<TVozilo>;

I messed the entities when I exported them again from the Data modeler tool.

I prepared a script in Data modeler to fix the issue in the future :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.