1

I think it is just my thinking that is off here. Suppose I have two entities: question and choice. A question can be a parent or child, or both (in relation to other questions). It seems that this model is too simple and is an unresolved many-to-many, but I think my thinking is the problem here so this is more of a conceptual question. What is the best way to design/model this? Please explain why it is or isn't a many-to-many relationship, and whether or not it needs to be resolved and why. See below:

idea

A question can be a parent or a child, or both (potentially multiple times). Programming logic would prevent the child from being its own parent and vice versa. It seems I'm missing a linking solution of some sort here, or I'm just not wrapping my brain around this.

3
  • 1
    Can a question have multiple parents? Is there a depth limit to parent-child relationship? Commented May 10, 2013 at 5:36
  • yes depth of 1 only. A child can only have one parent. But that parent could also be a child. Commented May 10, 2013 at 5:57
  • possible duplicate of Representing a parent/child hierarchy in a Relational Database Commented May 10, 2013 at 9:22

1 Answer 1

3

If a child can only have 1 parent it's not a many-to-many relationship, it's just 1 to many.

You could do this by simply adding a parent_id to the question:

Question: - id - parent_id (Contains the id of a parent if this is a child) - title - text

This is all you need, to get all childs to a parent just query for all questiong which have that specific parent_id. To get a parent from a child just use the parent_id column.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.