How do I use master/detail when a new record is being inserted in master?

I am using two TXDataWebDataSet, a master/detail relationship, when I am inserting a new record in the master the table ID does not exist yet, when doing the relationship below the ID is reset and an error occurs saying that there is no ID zero ( 0).

How do I use master/detail when a new record is being inserted in master?

TJSObject(wdsDetail.CurrentData)['PEOPLE@xdata.ref'] := Format('PEOPLE(%d)', [wdsMasterID.AsInteger]);

If the master doesn't exist, you cannot use @xdata.ref, because you still don't have the id.
You should create a property named PEOPLE which should contain a TJSArray of all the person objects.

Probably you are already doing that, so all you should not is to not set such PEOPLE@xdata.ref property.

Do you have a complete example of how to do this?

Because the array that is being manipulated is in the Detail TXDataWebDataSet, and the post of the Person Object is in the Master TXDataWebDataSet.

The array is in the master dataset. If PEOPLE property is a list, then probably there is a property in the master dataset named PEOPLE which holds the array.

Yes, there is an example in XData demos folder, subfolder web\master-detail, demo EDProject.groupproj.

This example XData demos folder, subfolder 'web\master-detail does not work with Insert, it does not record the detail, only the master.

In this example it works only when you change a record that already exists, which already has an ID.

Do you have any other examples that work with Insert with Master/Detail?

The demo is indeed different as it requires saving the master before adding the detail. To insert details together with data, you can do something like this:

TJSObject(wdsDetail.CurrentData)['PEOPLE'] := New(['$ref', 1]);

and then somewhere add the $id: 1 property to the master, like:

procedure TForm3.wdsMasterNewRecord(DataSet: TDataSet);
begin
  TJSObject(wdsMaster.CurrentData)['$id'] := 1;
end;

But I can't set the ID in the master, the ID is a sequence in my Aurelius entity.

In XData I don't need that, I just send the Object with the Master and the Array of the Detail and Aurelius that is in charge of cascading the ID of the Master to the Details.

See that in my payload there is no Array with the Detail of ID_ENDERECO and ID_CLIENTE as the Object PESSOA_CATEGORIA.

Thank's Wagner

Guys so no one is suffering like me.

When you want to do a Master/Detail where you have an Aurelius entity mapping its ManyAssociation with ID using sequence or autoincrement in WebCore you do the following:

Master = TXDataWebDataSet - cdsCrud
Detail = TXDataWebDataSet - cdsCliente, cdsEndereco

AfterInsert

procedure TFClienteEdit.cdsCrudAfterInsert(DataSet: TDataSet);
begin

inherited;

   // Details dataset
   cdsCliente.Insert;
   cdsEndereco.Insert;
end;
procedure TFClienteEdit.cdsCrudNewRecord(DataSet: TDataSet);
begin
   inherited;
   cdsCrudID_CLIENTE.Value := TJSArray.new;
   cdsCrudID_ENDERECO.Value := TJSArray.new;
end;

procedure TFClienteEdit.cdsCrudAfterScroll(DataSet: TDataSet);
begin
   inherited;
   if DataSet.State <> DsInsert then
   begin
     cdsCliente.Close;
     cdsCliente.SetJsonData(cdsCrudID_CLIENTE.Value); //cdsCrudID_CLIENTE is my ManyAssociation in Aurelius
     cdsClient.Open;

     cdsAddress.Close;
     cdsAddress.SetJsonData(cdsCrudID_ENDERECO.Value); //cdsCrudID_ENDERECO is my ManyAssociation in Aurelius
     cdsAddress.Open;
   end;
end;

The key point is here, the passage from the Array that is in your Detail DataSet to the Array that is in the Master

procedure TFClienteEdit.btSaveClick(Sender: TObject);
begin

   if cdsCliente.State in dsEditModes then
     cdsCliente.Post;

   if cdsEndereco.State in dsEditModes then
     cdsEndereco.Post;

   //This is the key point
   cdsCrudID_CLIENTE.Value := TJSArray.new(TJSJSON.parseObject(TJSJSON.stringify(TJSObject(cdsCliente.CurrentData))));
   cdsCrudID_ENDERECO.Value := TJSArray.new(TJSJSON.parseObject(TJSJSON.stringify(TJSObject(cdsEndereco.CurrentData))));

   inherited;
end;

It's $id, not id. It represents the instance id.

But I'm glad you solved your issue.