Subject How do I make ClientDatasets with master/detail relationships work right?
Author masonwheeler
I just ran into a bit of a problem. I've got two TIBOTables, let's call them M and D. The database table for D has a foreign key that points to M's table.

I have two TClientDataset instances, one linked to M and one linked to D, through providers. (Let's call them cdsM and cdsD.) The providers both have ResolveToDataset set to true, which means that when I call ApplyUpdates, the updates should only be sent to the TIBOTable dataset and not to the database.

Everything works fine until I try to delete a record in cdsM that has records in cdsD pointing to it. I deleted those as well, but when I run the following code:


...which should apply the updates back to the datasets and not to the database, I get a foreign key violation from the database. (And if I were to apply the updates in the opposite order, it would error on inserts to M instead. And this is a UI-driven operation, where it's quite possible to get both inserts and deletes in the same batch of updates.)

How do I set this up so that I can apply both inserts and deletes from the client datasets, and the TIBOTables will delay passing the SQL on to the database until it has all the necessary information it needs to handle things in the proper order? (Delete from D before M, insert to M before D.)