Understanding CahcedUpdates

Discussion of open issues, suggestions and bugs regarding SDAC (SQL Server Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
brace
Posts: 227
Joined: Wed 14 Feb 2007 08:26

Understanding CahcedUpdates

Post by brace » Thu 30 May 2013 14:04

This is the unexpecting behavior I am having:
I have Two tables you can simply call it Master Detail kinda attangement.

In my application the user creates a Master record and a corresponding detail records and finally save it.
for this purpose i used cachedupdates = true property.
It works fine if updates were for insertion,, wihle it fails if any Master record deleted.

if runtime user delete any master record i do

Code: Select all

sqlDetail.Filtered := false;
        sqlDetail.Filter := 'ID_MASTER = '+IntTostr(IdMaster);
        sqlDetail.Filtered := true;
        sqlDetail.First;
        while not sqlDetail.Eof do
          sqlDetail.Delete;
        sqlDetail.Filtered := false;
        sqlDetail.CheckBrowseMode;
        // Finally delete record in Master Table
        sqlMaster.First;
        SearchRecord([sqlMasterID_Master], [IdMaster]);     // My function to search Idmaster in query
         sqlMaster.Delete;


Then on Save button i perform following.

Code: Select all

try
        dbc.StartTransaction;
        sqlMaster.ApplyUpdates;
        sqlDetail.ApplyUpdates; // Here i have FK error
      except
        if dbc.InTransaction then
          dbct.Rollback;
      end;
===========================================00

By the way on:

ms-help://embarcadero.rs_xe2/Devart.SDAC/SDAC/Devart.Dac.TMemDataSet.CachedUpdates.htm

I read
Note: When establishing master/detail relationship the CachedUpdates property of detail dataset works properly only when TCustomDADataSet.Options is set to True.
Which options I need to set true?

AndreyZ

Re: Understanding CahcedUpdates

Post by AndreyZ » Mon 03 Jun 2013 08:44

The option that is mentioned in the description of the CachedUpdates property is LocalMasterDetail. We already fixed this problem with the SDAC documentation.
There are two ways of establishing master/detail relationship: using parameters, and using the MasterFields and DetailFields properties. You can find descriptions and examples of both ways in the "Master/Detail Relationships" article in the SDAC documentation. As you do not use any of these ways, you do not have an actual master/detail relationship. Therefore, LocalMasterDetail is not used in your case.
The problem you encountered occurs because you apply updates of the sqlMaster dataset before applying updates of the sqlDetail. To avoid the problem, you should apply updates of sqlDetail before applying updates of sqlMaster. Here is a code example:

Code: Select all

dbc.StartTransaction;
try
  sqlDetail.ApplyUpdates;
  sqlMaster.ApplyUpdates;  
  dbc.Commit;
except
  dbct.Rollback;
end;

Post Reply