yes, many thanks I now have a method in my Group class (still in elixir syntax)
def hierarchy_ng(self): with CommonTableExpression.create(" all_parents", ["id", "rank"]) as all_parents: rank = literal_column("rank") groups = Group.table groups_assoc = Group._descriptor.find_relationship('parents').table s = select([groups.c.id, literal_column("1")]).\ where(groups.c.id==groups_assoc.c.parents_id).\ where(groups_assoc.c.children_id==bindparam("groupid")).\ correlate(None).\ union( select([groups.c.id, rank + literal_column("1")]).\ where(groups_assoc.c.children_id== all_parents.c.id).\ where(groups_assoc.c.parents_id==groups.c.id ).\ correlate(None) ).params(groupid=self.id) all_parents = SelectFromCTE(all_parents, s) all_parents = all_parents.alias() q = Group.query.join((all_parents, all_parents.c.id==Group.id)) return q.all() and it's ok now ! -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/ji9w6r2l09IJ. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.