Master and Detail in Delphi, with Schemaadapter, only recorded the second time onwards

Asked

Viewed 477 times

0

I am using Delphi 10.1, Firebird 2.5 with Firedac. I have 3 Fdquery, all with Cachedupdates = true, being 1 MASTER, and 2 Detail. In the bd, the FK points to the ID of the master table. In the application, mastersource, Masterfield and Indexfieldname filled as per this link.

I use a Tfdschemaadapter, all 3 Querys have the Schemaadapter field pointing to the Fdschemaadapter1. Until then ok, it works, takes the key at the time of recording and stores in the two query Detail. However, using the code below to permanently save to the comic, from the first attempt it triggers error (1 error for each line in the details), and worse: persists the recording in the MASTER table, and does not record anything in any of the 2 Detail.

Then, I close the form, remaining with the application open, open the form again, put new data in all 3 query, and when firing the code below, it works as expected, triggering 0 error, and works ok all other 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;

1 answer

0

I solved it. More than one error was causing this. The first one, a Form that was loading with the application left a Transaction open (it was in Autocreate, not in Delphi’s avaliable Forms), although nothing in this form triggers a Starttransaction unless it was clicked, checking with the Intransaction command I noticed an open transaction. Moving this form to "avaliable Forms" solved.

Fixing this, there was still another problem when I call, in Master’s sql "A.ID the NICKNAME" and put the NICKNAME field in the MASTERFIELD of the Detail querys, apparently works, but gives error when sending the command Fdschemaadapter1.ApplyUpdates. If I leave it in the master sql simply A.ID, and use the ID field to assign to the MASTERFIELD of the Detail querys, 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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.