The Master/Detail Behavior

2019-07-28 13:34发布

问题:

I use Delphi 7 + Zeos + MySQL, and I got a problem when I try to post Master table, the Details datasets are posted first, raising a referencial integrity exception in data base, 'couse details tables needs the ID of the Master table.

Can I revert this behavior? Can I persist the master table before the details?

回答1:

I think it's just the way TDataSet work. If you have unposted detail records, master.Post forces them to Post if I remember correctly.

So I am guessing you have something like:

tblMaster.Insert;
tblMaster.FieldByName('foo').Value := 'foo';
tblDetail.Insert;
tblDetail.FieldByName('bar').Value := 'bar';
tblMaster.Post; // error!
tblDetail.Post;

You should be able to work around this by posting master first:

tblMaster.Insert;
tblMaster.FieldByName('foo').Value := 'foo';
tblMaster.Post;
tblDetail.Insert;
tblDetail.FieldByName('bar').Value := 'bar';
tblDetail.Post;