Delphi - how to handle delete in the master-detail relationship?

1

I have two tables using Firedac TFDTable and have made 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 would like it to automatically delete the corresponding records in the Dbgrid detail, with a deletion warning before deleting, so that the user confirms his intention. How to do this validation and delete operation? Thanks for the help.

    
asked by anonymous 26.09.2017 / 21:13

2 answers

1

It's possible, but I do not recommend it. I actually do not recommend working with Tables , it is very tied up and it 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 this link there is an explanation from Embarcadero on how to do it you want. In short you should follow these steps:

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

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

Another possibility that I find most convenient is through foreign keys cascade .

    
26.09.2017 / 22:35
1

In addition to the previously described possibility you can also do 'manually' through the BeforeDelete event of the TFDTable master and put the confirmation message and a loop to delete detail lines such as:

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

It can give more work but allows you to have more control.

    
26.09.2017 / 22:50