Canonical Id to simple Id


I'm well aware of the advantages of a Canonical Id representation

and I'm getting this result from a REST call to XData Server with DefaultExpandLevel set at 0:

  "fk_plant_id@xdata.ref": "COM_plants(2)",
  "fk_dept_id@xdata.ref": "COM_enx_depts(4)",
  "fk_dept_id_alt": null,
  "fk_prodphase_id": null,
  "fk_prodphase_id_alt": null,

so: when there's no association, the fk_ foreign key field points to null; otherwise it points to a "Canonical Id-ized" record and the field name is modified with an appended @xdata.ref tail.

Is there a way to get a response with "fk_plant_id": "2"
instead of "fk_plant_id@xdata.ref": "COM_plants(2)" from XData?

At present my client code (angular with dxDataGrid from DevExpress) expects a response with "simple" Id for associations and does a "local" lookup with cached data... that's why "simple" Ids would be preferred during this migration phase...

No, that is not possible with automatic CRUD endpoints automatically.
You can force the canonical id to be inlined, so the response will be like:

"fk_plant_id": {
   "id": "2"

Among other existing properties in fk_plant_id object.
Alternatives is if you create a public property that is a getter for the id and flag with the XDataProperty attribute, something like:

TMyClass = class
  function GetPlantId: Integer;
  property plant_id: Integer read GetPlantId;


function TMyClass.GetPlantId: Integer;
  Result := fk_plant_id.Id;

Or, just use the most flexible option which are service operations. Then you can simply define your DTO return class the way you want with the format and properties you want.

Using [XDataProperty] works great in READING, I've added a "new" field aside the "Canonical Id old one" and so I need very little changes in my existing front-end angular code (that receives the value 4 in the xfk_plant_id field now)


What is not clear to me is how to "directly" change the foreign key... I understand that with "Canonical Id" if I want to change the association from the 4 to 6 a PATCH with the following body does the job

{  "fk_plant_id@xdata.ref": "COM_plants(6)" }

but since my front-end sends a PATCH with a "simple, not canonical" body like { "xfk_plant_id": 8 } is there a direct way to make the property read/write with a write method that simply accepts an Integer and updates not the Proxy object but just the pointer to it?
...something like procedure Set_xfkPlantId(const Value: Nullable<Integer>);

I'm feeling myself so newbie with XData... :thinking:

That is not easy (or even possible) to accomplish using automatic CRUD operations.
If you have very specific needs (why?) for sending data to the server and processing them, the easiest and recommended way is just create a service operation which receives a specific DTO class with the properties you specify, and then freely implement the code that process this request, i.e., check the FK raw value sent in the object and properly setting it in the database.

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