Unexpected Identity field value


I have TEchoServerModule on a Datamodule in my xData restserver with MSSQL db. Using Route I'm only replicating 1 entity (DemoTable). This Entity looks like:

  [Id('FID', TIdGenerator.IdentityOrSequence)]
  TDemoTable = class
    [Column('ID', [TColumnProp.Required, TColumnProp.NoInsert, TColumnProp.NoUpdate])]
    FID: Integer;

    [Column('Name', [], 50)]
    FName: Nullable<string>;

    [Association([TAssociationProp.Lazy], CascadeTypeAll - [TCascadeType.Remove])]
    [JoinColumn('ParentID', [], 'ID')]
    FParentID: Proxy<TDemoTable>;
    function GetParentID: TDemoTable;
    procedure SetParentID(const Value: TDemoTable);
    property ID: Integer read FID write FID;
    property Name: Nullable<string> read FName write FName;
    property ParentID: TDemoTable read GetParentID write SetParentID;

For testing I've used your EchoTestDemo project to point to my server and using the correct model/entity.

With both my rest Server and client running, I POST a new record into DemoTable using my rest Server's Swagger ui. Record is posted successfully, and values are confirmed in my MSSQL db:
ID = 27, ParentID = NULL, Name = "First Record"

Now, when refreshing Client1 and Client2 (in the EchoTestDemo project), I see a newly replicated record with field Name = "First record" as expected, BUT field ID = 1 (Not ID = 27 as I'd expect).

Any hints or tips as to what I'm doing wrong or misunderstanding here?

Thank you in advance.

You should remove the TColumnProp.NoInsert flag from the FID field, otherwise it will never be present in the INSERT SQL statement executed by Echo when the record is loaded:

    [Column('ID', [TColumnProp.Required, TColumnProp.NoUpdate])]
    FID: Integer;

Ok, I have now removed the TColumnProp.NoInsert and now the fields ID and Name are replicated just fine, thank you.

But I seem to have an issue with the last field, ParentID. As seen from the model this field is a FK to the
same table. Too me it looks like the field ParentID "gets" an incorrect value client-side.

To demonstrate my issue I deleted all Echo tables and db's on server and client side, and emptied the DemoTable in my server db (MSSQL). So from a completely clean start on both server and client side I do this:

  1. Start my xData rest server.

  2. Start my client demo project (your EchoTestDemo).

  3. Using swagger I POST an initial new record to the DemoTable/Enity on the sever, with values:
    (PS. the autogenerated value of ID becomes 30)

  4. Using swagger I POST a second record to the DemoTable/Enity on the server with values:

  5. Data are confirmed in my server DB correctly:

  6. But the data replicated to my client(s) look like this:

As you can see the value (client-side) of the field DemoTable.ParentID is not 30 as I would expect.

So, my questions is again (sorry :)) what I am I'm overlooking or not doing right here?

This is TAureliusDataset behavior. The ParentID field is actually an object of type TDemoTable, not Integer:

property ParentID: TDemoTable read GetParentID write SetParentID;

Thus, the dataset will show the pointer to the object, not the actual ID field value. Since they are effectively different object instances, their values will be different.

To see the parent ID value, create a dataset field named ParentID.ID and you will be able to see it (and you can use the same approach to see other properties, like ParentID.Name).

Ah, of course. I keep missing basic functionality, guess it's a result of doing too many things at once :blush:
Thank you for your great support, and your patience.

1 Like

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