Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Use Case

What are the use cases for creating a one-to-one relationship where the parent points to the child compared to a child pointing to the parent? If there is no difference, is one preferred over the other?

Example

+---------+     +-------+       +--------+     +----------+
+ Parent  +     + Child +       + Parent +     + Child    +
+---------+     +-------+  vs.  +--------+     +----------+
+ idChild + --> + id    +       + id     + <-- + idParent +
+---------+     +-------+       +--------+     +----------+

I suspect that you'd choose between these relationships depending on which entity is used first in the application. i.e. If the parent object always appears before the child object, then the relationship should be mapped from the parent to child.

share|improve this question
    
Please explain down vote. It will help me structure questions better. –  Pete Mar 20 at 15:52

3 Answers 3

up vote 1 down vote accepted

It can effect the representation of your structure if you marshal it to XML or JSON. For example, do you want "parent" to be the top element or object, or do you want "child" to be the top element or object?

A lot of times it depends on how the application uses the data. If the primary users of a system are children, it may be more convenient to represent child -> parent because you're doing the lookup starting with the child ID.

share|improve this answer

In terms of database design is quite more logical to have the child store the relationship back to the parent. Primarily, because keeping the information for each child in the parent's is inflexible (assuming you use fields for child's IDs), like in your first example, since you cannot dynamically allocate more than a single reference to a child.

share|improve this answer
    
Is there a use case where it is better for the parent would point to the child? –  Pete Mar 20 at 16:16
1  
If you talking outside of database design, then yes. There are some scenarios where the parent needs to control or manage children without their explicit consent or knowledge. However, this is something that can be implemented in your domain or business logic without affecting you underlying database design. –  rae1 Mar 20 at 16:44

I was working on a software project with one-to-one relationships, in our project was the child pointing on the parent, because it just is more logical, that the child shows its parent and not other way round.

share|improve this answer
    
Is it because a parent may or may not have a child? Whereas a child must have a parent, so it's more optimal to model the child pointing to its parent. –  Pete Mar 20 at 15:41
    
Yeah sth like that –  Knerd Mar 20 at 15:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.