MasterKey change and update in detail


(ionescu) #1

Hello.

I have a bunch of Tables in DA Schema that are in a master-detail relation. One example is customer table and activity table, customer being the master table. My primary key in the Customer table is a string. What i want to do is something similar to what dataabstract does with the generator and the AutoInc fields, meaning that in the update process it also changes the keys in the detail table so it matches the new master key. Can something like this be achieved?


(antonk) #2

Hello

What is the client and server platforms?

Am I right that you need to implement the following scenario:

  1. Client-sde some rows are inserted into the master and detail tables, with master key field set to some placeholder value
  2. Update is sent to the server
  3. Server-side a proper master key value is generated based on some rules and then both master and detail rows are inserted with this proper master key value.
  4. Client retrieves information about hte real master key value and updates both detail and master rows

Regards


(ionescu) #3

The client is a javascript application and the server is written with delphi.

The scenario is right till point 3. I use a placeholder as the key. As i mentioned i try to achieve something similar to the AutoInc field and Generator. I also have tables with Integers as primary key and the key’s are changed in both master and server, but in this particular case i need to use a string as a key.

Tried with a custom insert command in the schema but that inserts the master rows with the new key but my detail rows have the old placeholder.


(EvgenyK) #4

DA server can autoupdate records only for AutoInc fields.

I can recommend to handle your case manually like:

  • client sends delta with new master record to server
  • server processes it and returns new master key
  • client changes master key in all detail records and sends changes to server

if your scenario allows to update master key in existed master records, it can be a bit easier:

  • client performs updates for detail table(s) with old master key
  • client send delta for master table and server returns new master key
    • all records in detail table(s) are updated with new master key via DB trigger or via DA server-side event
  • client just re-fetches changes for detail table(s) from server

(ionescu) #5

Isn’t there any method to extend the system that works for AutoInc to work for strings as well. I already thought about a possible solution, meaning that i have to manually change the detail keys in the BeforeProcessDelta event but that means that i have to implement the changes for all my tables while if i extend the system that works for AutoInc the framework will handle it as a natural behavior


(EvgenyK) #6

there is no method what you can extend easily.

you can try to implement this workaround:

  1. you store all deltas into local variable in DAService.OnBeforeProcessDeltas event
  2. in DAService.BusinessProcessorAutoCreated you assign to newly created BusinessProcessor, OnBeforeProcessChange event
  3. in above event (OnBeforeProcessChange) you should detect if change belongs to details table and you can change master key.

Note: in 3rd step you should use TDADeltaStructList that is stored in 1st step and ServiceSchema.RelationShips that should store foreign keys. foreign key gives you information about master table for given table and fields in master key