Gajus
Gajus

Reputation: 73738

The Nested Set Model hierarchical data

I've a simple question.

My database structure is:

id | name | left | right // no need to tell me that left, right are reserved keywords

The only data in the database:

1 | family | 1 | 2

Now, I want to add new child to the family: mother. So theoretically I should take the right value of the element to which I want to add the child and free-up some space after it.

UPDATE `hp_tree` SET `right`=`right`+2 WHERE `right` > 2;
UPDATE `hp_tree` SET `left`=`left`+2 WHERE `left` > 2;

And then simply insert the child mother:

INSERT INTO `hp_tree` SET `left`=2, `right`=3, `name`='Mother';

Now, the problem is, that this way the family (root element) right value is not updated. Am I doing it wrong?

Upvotes: 0

Views: 1548

Answers (1)

Gajus
Gajus

Reputation: 73738

Ok. I found this explained in Managing Hierarchical Data in MySQL. See the part starting with If we instead want to add a node as a child of a node that has no existing children.

Upvotes: 1

Related Questions