Delphi - how to treat delete in the master-Detail relationship?

Asked

Viewed 749 times

1

I have two tables using Firedac Tfdtable and did the Master-Detail relationship between them which works well. In Forms I have two Dbgrids, one for each table , when deleting a record in Dbgrid Master I would like it to automatically erase the corresponding records in Dbgrid detail, with an delete warning before deleting, so that the user confirms his intention. How to do this delete validationand operation? I appreciate the help.

2 answers

1


It’s possible, but I don’t recommend it. I don’t really recommend working with Tables, is very tied and limits quite a lot of technical aspects that can be improved in the application. But that’s up to everyone, it’s just a personal opinion.
In that link there is the explanation arising from Embarcadero how to do what you want.
In short you must follow the following steps:

  • Drop a TFDSchemaAdapter on a form.
  • Set the property SchemaAdapter of master dataset to the TFDSchemaAdapter.
  • Set the property SchemaAdapter of dataset detail to the TFDSchemaAdapter.

  • Set the property FetchOptions.DetailCascade of dataset detail for True.

Another possibility I find more convenient is through Foreign Keys.

  • Andrey, thank you. I will study this proposal and prototype a template to evaluate how it works. I will then respond with the result.

  • Andrey, finish the test. I put the settings described in the link above , I did step by step in a test form, but IT DID NOT WORK , because when trying to delete a record in Dbgrid Master, which had children returned the error [Firedac][Phys][Ora] ORA-0922: Integrity Cont (JRG.Depto_emp_fk) violated - Child record found. I also changed the property of the Fdqueryemp (Detail) Detailservercascade = true , but nothing has changed ! I appreciate if you can help me !

  • Important : in this test I used Fdquery instead of Fdtable, even then it doesn’t work !

  • I found that I had to configure the property Cacheupdates=True in both datasets. Now IT WORKED delete Scade between a Dbgrid Master and a Dbgrid Detail ! Andrey , thanks !

1

In addition to the possibility already described above you can also do 'manually', through the event Beforedelete of Tfdtable master and place the confirmation message and a loop to erase the lines of the Detail as an example:

detail.DisableControls;
While detail.Locate('idpai',masterid.value,[]) do 
    detail.delete;
detail.EnableControls;

It can take more work but allows more control.

  • Tiago, I understood the proposal. I will simulate and then I answer what happened. Thanks, for now !

Browser other questions tagged

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