What database are you using?

Are you trying to solve data insert or retrieval?

Do you want to do your traversal in your app or use SQLAlchemy to generate
a SQL query that does all the work within the DB and then returns the
result?


ᐧ

On Wed, Dec 16, 2015 at 1:28 PM, Horcle <g...@umn.edu> wrote:

> I have the following SQLAlchemy class representing an adjacency list:
>
> class Node(db.Model):
>     __tablename__ = 'meds'
>     id = Column(Integer, primary_key=True)
>     type = Column(String(64))
>     name = Column(String(64))
>     parent_id = Column(Integer, ForeignKey('node.id'))
>     children = relationship("Node")
>
> I need to create a dictionary to represent a tree of arbitrary depth that
> would look like:
>
>
> {
> "children": [
>     {
>       "children": [
>         {
>           "id": 4,
>           "name": "Child1",
>           "parent_id": 3,
>           "type": "Parent 2"
>           "children": [
>             {
>               "id": 6,
>               "name": "Child3",
>               "parent_id": 3,
>               "type": "Parent 3",
>               "children": [...]
>             },
>             {
>               "id": 7,
>               "name": "Child4",
>               "parent_id": 3,
>               "type": "Leaf"
>            }
>           ]
>         },
>         {
>           "id": 5,
>           "name": "Child2",
>           "parent_id": 3,
>           "type": "Leaf"
>         }
>       ],
>       "id": 3,
>       "name": "CardioTest",
>       "parent_id": null,
>       "type": "Parent"
>     }
>   ]
> }
>
>
> Can this dictionary be built non-recursively? I am not sure how to
> manually do this otherwise.
>
> Thanks in advance!
>
> Greg--
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
>



-- 

*Jeff Widman*
jeffwidman.com <http://www.jeffwidman.com/> | 740-WIDMAN-J (943-6265)
<><

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to