Foreign Key to same table -- any issues?

Posts   
 
    
KL
User
Posts: 11
Joined: 31-Aug-2006
# Posted on: 22-Jan-2007 08:52:29   

Hey,

I haven't done much "database design", but I want to create a group hierarchy of sorts. So, I have a "Groups" table (with "GroupID" as the PK), and one of the fields is a "ParentGroupID" field which is a foreign key to the "GroupID" field. The idea is that you can build an infinite (in theory) depth of groups in a hierarchy. In general, is this bad practice? And specifically, will any issues arise with LLBLGen when doing this? And in the designer, I assume I need to hide one of the relations (since it will create a duplicate relation under the same entity since it's a relation to itself) -- does it make a difference which one I hide (since they both appear to be the same, which sort of makes sense)?

Thanks.

mihies avatar
mihies
User
Posts: 800
Joined: 29-Jan-2006
# Posted on: 22-Jan-2007 09:24:13   

There is nothing wrong with this practice. I don't know if there are any problems with LLBLGenPro but I doubt there are any.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 22-Jan-2007 09:38:15   

Depends on what you want to do with the data: if you want to fetch the hierarchy in 1 go, it's not possible, so you have to fetch all data and build the hierarchy in memory. See: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=7984

IF you are using selfservicing, don't hide any relationships. If you're using adapter, hide PK->FK, but not the FK -> PK one.

Frans Bouma | Lead developer LLBLGen Pro
KL
User
Posts: 11
Joined: 31-Aug-2006
# Posted on: 22-Jan-2007 09:45:15   

thanks for the replies guys... I'm actually using Adapter. How can I tell which is the PK->FK relationship and which one is the FK->PK one? they appear the same to me (I'm using 2.0 and I'm still relatively new to LLBLGen)

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 22-Jan-2007 10:30:19   

KL wrote:

thanks for the replies guys... I'm actually using Adapter. How can I tell which is the PK->FK relationship and which one is the FK->PK one? they appear the same to me (I'm using 2.0 and I'm still relatively new to LLBLGen)

A 1:m B, then A is the PK side, B is the FK side. B m:1 A, then B is the FK side, and A is the PK side. A 1:1 B, you've to check the relation itself, the side where the field is the FK, is the FK side.

Frans Bouma | Lead developer LLBLGen Pro
KL
User
Posts: 11
Joined: 31-Aug-2006
# Posted on: 22-Jan-2007 10:38:48   

Otis wrote:

KL wrote:

thanks for the replies guys... I'm actually using Adapter. How can I tell which is the PK->FK relationship and which one is the FK->PK one? they appear the same to me (I'm using 2.0 and I'm still relatively new to LLBLGen)

A 1:m B, then A is the PK side, B is the FK side. B m:1 A, then B is the FK side, and A is the PK side. A 1:1 B, you've to check the relation itself, the side where the field is the FK, is the FK side.

got it, thanks... i mistakenly setup my foreign key incorrectly (i had the primary key set as the foreign key pointing to itself frowning ) which is why I couldn't see the difference in the designer...