dsonda
dsonda

Reputation: 71

Wrong nested datasets insert order on ApplyUpdates

I have 4 ClientDataSets like this:

Master

---Detail 1

---Detail 2

------SubDetail 2.1 - here there is a FK to Detail 1

The insert order of records on datasets is: Master, Detail 1, Detail 2, SubDetail 2.1.

However, the insert order on database, when I call ApplyUpdates, is Master, Detail 2, SubDetail 2.1, Detail 1.

So I get an error "Foreign key reference target does not exist", because there is a FK on SubDetail 2.1, that point to a record on Detail 1, which isn't inserted on database yet.

Can I change the post order of nested datasets on database? Or is there another way to fix this problem?

Upvotes: 0

Views: 578

Answers (1)

bummi
bummi

Reputation: 27385

You can use the following on your datasets

procedure TTemplate.MasterpostBeforeInsert(DataSet: TDataSet);
begin
  if Assigned(Dataset.DataSource) and Assigned(Dataset.DataSource.DataSet) then
    if Dataset.DataSource.DataSet.State in [dsInsert] then 
      Dataset.DataSource.DataSet.Post;
end;

Upvotes: 0

Related Questions