Master-Detail & CachedUpdates: foreign key not auto filled

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
Opet
Posts: 1
Joined: Sun 03 Oct 2010 08:02

Master-Detail & CachedUpdates: foreign key not auto filled

Post by Opet » Sun 03 Oct 2010 08:09

Please look at this simple case. I have 2 MySQL tables:

Master
======
IdMaster int autoinc
FMaster varchar(100)


Detail
======
IdDetail int autoinc
IdMaster int
FDetail varchar(100)


I have 2 TUniQuery components, qMaster and qDetail. They were set up this way:

Code: Select all

qMaster.CachedUpdates:=true;
qMaster.SQL.Text:='select * from Master';

qDetail.CachedUpdates:=true;
qDetail.SQL.Text:='select * from Detail where IdMaster = :IdMaster';
qDetail.DataSource:=dsMaster; //dsMaster is the DataSource of qMaster
qDetail.Options.LocalMasterDetail:=true; //tried with and without this

Now here is the problematic code:

Code: Select all

qMaster.Open;

qMaster.Append;
qMaster['FMaster']:='this is master rec A';
qMaster.Post;

qDetail.Open;

qDetail.Append;
qDetail['FDetail']:='this is detail rec #1 of master rec A';
qDetail.Post;
qDetail.Append;
qDetail['FDetail']:='this is detail rec #2 of master rec A';
qDetail.Post;

qMaster.ApplyUpdates;
qDetail.ApplyUpdates;
The IdMaster field in the Detail table don't filled automatically. How to make it get filled automatically?

Thank you.

AndreyZ

Post by AndreyZ » Mon 04 Oct 2010 10:52

Hello,

When you are using CachedUpdates, your master-table doesn't get data from the server. And your master-field is autoincrement, which means that you want server to give value for this field. To solve the problem you should change your code to the following:

Code: Select all

  qMaster.Open;
  qMaster.Append;
  qMaster['FMaster']:='this is master rec A';
  qMaster.Post;
  qMaster.ApplyUpdates; // here you are retrieving the IDMaster field value from the server

  qDetail.Open;
  qDetail.Append;
  qDetail['FDetail']:='this is detail rec #1 of master rec A';
  qDetail.Post;
  qDetail.Append;
  qDetail['FDetail']:='this is detail rec #2 of master rec A';
  qDetail.Post;
  qDetail.ApplyUpdates;

Suhaimin
Posts: 79
Joined: Mon 06 May 2013 12:19

Re: Master-Detail & CachedUpdates: foreign key not auto filled

Post by Suhaimin » Mon 20 May 2013 00:22

i tried 2 table in postgresql

Master
======
IdMaster int
FMaster varchar(100)


Detail
======
IdDetail int
IdMaster int
FDetail varchar(100)


I have 2 TUniQuery components, qMaster and qDetail.

Code: Select all
qMaster.CachedUpdates:=true;
qMaster.SQL.Text:='select * from Master';
qDetail.Options.LocalMasterDetail:=true;

qDetail.CachedUpdates:=true;
qDetail.SQL.Text:='select * from Detail where IdMaster = :IdMaster';
qDetail.DataSource:=dsMaster; //dsMaster is the DataSource of qMaster
qDetail.Options.LocalMasterDetail:=true; //tried with and without this



onnewinsert qmaster :
qmasterIdmaster.value := 100 // manually;


onnewinsert qdetail :
qdetailidmaster.value := qmasteridmaster.value;


Tdbgrid connect to qdetail. i entry data via dbgrid but after post (move to next row of tdbgrid) the data that i entry missing from dbgrid. why this happened ?
thanks a lot.

DemetrionQ
Devart Team
Posts: 271
Joined: Wed 23 Jan 2013 11:21

Re: Master-Detail & CachedUpdates: foreign key not auto filled

Post by DemetrionQ » Mon 20 May 2013 14:13

Hello.

I answered to you at the theme
  http://forums.devart.com/viewtopic.php?f=28&t=27112

Suhaimin
Posts: 79
Joined: Mon 06 May 2013 12:19

Re: Master-Detail & CachedUpdates: foreign key not auto filled

Post by Suhaimin » Tue 21 May 2013 00:38

thanks

DemetrionQ
Devart Team
Posts: 271
Joined: Wed 23 Jan 2013 11:21

Re: Master-Detail & CachedUpdates: foreign key not auto filled

Post by DemetrionQ » Tue 21 May 2013 16:42

If any questions come up, please contact us.

Post Reply