Problem with Multiple Foreign Key

Forum related to version 6.5.1 (alpha) and 6.6.x (beta) of ZeosLib's DBOs

Moderators: gto, cipto_kh, EgonHugeist

Post Reply
Olga
Fresh Boarder
Fresh Boarder
Posts: 3
Joined: 27.07.2007, 03:21

Problem with Multiple Foreign Key

Post by Olga »

Hi, friends.
I have a little problem.
Please, tell me "how can I create master-detail relation beetwen 2 tables with multiple foreign key".
______________________________
|table Farm:
|idFarm
|...
|______________________________
|table GrainInf:
|idGrainInf (PK)
|farm_idFarm (FK)
|...
|______________________________
|table QualityGrain
|GrainInf_idGrainInf (FK)
|GrainInf_farm_idFarm (FK)
|...
|______________________________

Properties MasterFieldS and linkedFieldS have only One Value
What can i do?

Best regards, Olya
pol
Senior Boarder
Senior Boarder
Posts: 91
Joined: 13.10.2005, 08:19

Post by pol »

Hi Olga,

Why do you need the GrainInf_farm_idFarm in QualityGrain? GrainInf is already linked to Farm, so when QualityGrain is linked to GrainInf (by GrainInf_idGrainInf) it is automatically linked to Farm.
It would be another thing if GrainInf would be "Farm-independent" (which in my opinion would make sense). Then QualityGrain would in fact have to be linked to two different tables. Interesting thing. I have no idea how to do that with Zeos, but I will give it a try.

Regards,
Rüdiger
Olga
Fresh Boarder
Fresh Boarder
Posts: 3
Joined: 27.07.2007, 03:21

Post by Olga »

Hi, Rüdiger.
Relations in this database are:
table Farm -> one to many table GrainInf
table GrainInf -> one to one table QualityGrain.

all keys from GrainInf pass to table QualityGrain automatically.
And I need create relation master-detail between:

idGrainInf
farm_idFarm

and

GrainInf_idGrainInf
GrainInf_farm_idFarm.

Regards,
Olga
pol
Senior Boarder
Senior Boarder
Posts: 91
Joined: 13.10.2005, 08:19

Post by pol »

Hi Olga,

Now I tried it. It can't be done automatically, because the "MasterSource" property of a TZQuery only accepts one. But it is not so bad. In the "OnChange" method of both components where you display the Farms and Grains I did that:

Code: Select all

QQualityGrain.Close;
QQualityGrain.ParamByName('FARMID').AsInteger:=TFarmFARMID.AsInteger;
QQualityGrain.ParamByName('GRAININFID').AsInteger:=TGrainInfID.AsInteger;
QQualityGrain.Open;
And the SQL of QQualityGrain looks like that:

Code: Select all

select * from qualitygrain where grainid = :graininfid and farmid = :farmid
and the DBText linked to QualityGrain.Quality shows the right value for the choosen combination of Farm/Grain (or nothing if there is no such combination).
I hope that is what you looked for.

Regards,
Rüdiger
gto
Zeos Dev Team
Zeos Dev Team
Posts: 278
Joined: 11.11.2005, 18:35
Location: Porto Alegre / Brasil

Post by gto »

There's no need to use OnChange. You can use the database property, like this:
http://zeos.firmos.at/viewtopic.php?p=2199#2199

If the relation between Grain and Quality is dependant from the farm then, to make this type of relationship better, even for selecting data in future reports, try to do this:

Code: Select all

  Farm    |   Grain    |  Quality
----------------------------------------
 FarmCode |  FarmCode  |  FarmCode
 ...      |  GrainCode |  GrainCode
          |  ...       |  QualityCode
		    |	         |  ...
If the relation between Grain and Quality is not dependant from the farm, then there's no problem at all, as they'll be all single relations.

By the way, be welcome Olga! There are almost no Women here, or at least, they don't post heheh
Use the FU!!!!!IN Google !

gto's Zeos Quick Start Guide

Te Amo Taís!
Olga
Fresh Boarder
Fresh Boarder
Posts: 3
Joined: 27.07.2007, 03:21

Post by Olga »

Thanks, friends. I'll try to use yours advice.
:D
I'm pleased to be the first woman in this forum
Hi-Hi-Hi
:oops:
Post Reply