Master and Detail in delphi, with SchemaAdapter, only records the second time onwards

0

I'm using Delphi 10.1, Firebird 2.5 with Firedac. I have 3 FDQuery, all with CachedUpdates = true, being 1 MASTER, and 2 detail. In bd, the FK points to the ID of the master table. In the application, mastersource, MasterField and IndexFieldName are filled in as this link .

I use a TFDSchemaAdapter, all 3 queries have the SchemaAdapter field pointing to FDSchemaAdapter1. Until then, it works, takes the key at the time of writing and stores it in the two query detail. However, using the code below to save permanent in the bd, the first attempt it fires error (1 error for each line in the details), and worse: recording continues in the MASTER table, and does not record anything in any of the 2 details.

Then, I close the form, remaining with the application open, open the form again, put new data in all 3 query, and when I trigger the code below, it works as expected, firing 0 error, and it works ok all the others times.

No error is triggered when I give append, insert, post.

Where am I going wrong?

//Código do botão para salvar 
dm.FDConnection1.StartTransaction;
iErrors := FDSchemaAdapter1.ApplyUpdates;
if iErrors = 0 then
  begin
    ShowMessage('não deu erros: '+IntToStr(iErrors));
    FDSchemaAdapter1.CommitUpdates;
    dm.FDConnection1.Commit;
  end else
    begin
      MsgBxErroWarn('Deu erro: '+IntToStr(iErrors));
      dm.FDConnection1.Rollback;
    end;
    
asked by anonymous 13.06.2018 / 17:48

1 answer

0

I have resolved. More than one mistake was causing it. The first one, a Form that was loading with the application left a Transaction open (it was in Autocreate, not in evaluable forms of Delphi), although nothing in this form triggered a StartTransaction unless it was clicked, checking with the InTransaction command I noticed an open transaction. Moving this form to "avaliable forms" solved.

Correcting this, there was still another problem when I call, in the Master sql "A.ID the SURNAME" and put the SURNAME field in the MASTERFIELD of the querys detail, apparently it works, but fails to send the FDSchemaAdapter1.ApplyUpdates command. If I leave in the sql of the master simply A.ID, and use the ID field to assign to the MASTERFIELD of the querys detail, it works.

if (dm.FDConnection1.InTransaction = False) then
begin
  dm.FDConnection1.StartTransaction;
end;
iErrors := FDSchemaAdapter1.ApplyUpdates(0);
if iErrors = 0 then
  begin
    ShowMessage('não deu erros: '+IntToStr(iErrors));
    fdqVendaCaixa.CommitUpdates;
    fdqVendaCaixaItens.CommitUpdates;
    fdqVendaPgtoC.CommitUpdates;
    dm.FDConnection1.Commit;
  end else
    begin
      MsgBxErroWarn('Deu erro: '+IntToStr(iErrors));
      dm.FDConnection1.Rollback;
    end;
    
13.06.2018 / 22:25