I am following the following tutorial on displaying hierarchical data from database http://docs.sqlalchemy.org/en/rel_0_7/orm/relationships.html#adjacency-list-relationships
so far i have the following table
class Node(Base):
__tablename__ = 'node'
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('node.id'))
data = Column(String(50))
parent = relationship("Node", remote_side=[id])
And the following entries in mysql
id parent_id data
1 NULL root
2 1 [->] child1
3 1 [->] child2
4 3 [->] subchild1
5 3 [->] subchild 2
6 1 [->] child3
7 NULL root2
8 NULL root3
9 7 [->] subchild0froot2
10 8 [->] subchildofroot3
11 1 [->] child4
I want to retrieve data in a format that will be suitable for comments e.g root -> child1 -> child2 ->(subchild1->subchild2)->child4
So far i have been able to retrieve the children of a parent through this query
nodealias = aliased(Node)
qry = session.query(nodealias,Node).\
join(nodealias, Node.parent).\
filter(and_(Node.postid==45))
print qry
for x,y in qry:
print x.data
print y.data
print "...."
And it displays
root
child1
....
root
child2
....
child2
subchild1
....
child2
subchild 2
....
root
child3
....
root
child4
....
I want to group this results in the following manner
root
....
child1
....
child2
subchild1
subchild 2
....
child3
....
child4
....