hello everyone! I was wondering which is the best way to model a tree-looking structure with SqlAlchemy.
Right now, I have this two classes: ---------------------------------------- #!/usr/bin/python2.6 class MyNode(object): def __init__(self): self.items = list() def addItem(self, item): if isinstance(item, MyNode) or isinstance(item, MyLeaf): self.items.append(item) def __str__(self): retval = str() for item in self.items: if isinstance(item, MyNode): retval = retval + "\n< Node: " + str(item) + " >\n" else: retval = "\nLeaf: " + str(item) + retval return retval class MyLeaf(object): def __init__(self): self.content = "I'm a leaf" def __str__(self): return self.content ---------------------------------------- As you can see, a node can contain (in its ".items" list) other nodes, other leafs and a mix of nodes and leafs. When I model that using SqlAlchemy, both MyNode and MyLeaf will be stored their own tables and will have a unique id field in the database. I was wondering if the best thing to do is using two relationships in the MyNode class or trying to do it with one (closer to how it is now). If I use two relationships, the first one (which I could call ".otherNodes", for instance) could give me the other instances of MyNode and the second one (".leaves", for instance) would give instances of MyLeaf. And well... I really don't know if it's doable putting both of them together in a unique relationship. I know I'm going to need intermediate tables to model this structure, but I don't know if it's doable (or good idea) to make only one table that may look like: nodes_towards_content = Table( "nodes_towards_content", metadata, Column("node_id", Integer, ForeignKey("nodes.id")), Column("other_node_id", Integer, ForeignKey("nodes.id")), Column("other_leaf_id", Integer, ForeignKey("leaves.id")) ) So if I insert a node, the column "nodes_to_content.c.other_node_id" would take the id of that inserted node while "nodes_to_content.c.other_leaf_id" would be null, and if I insert a leave, the process would be the opposite. If I use a unique table, I am also a bit concerned of how to retrieve the objects (what to tell the query, I mean). For the moment I have always had relationships that returned one type of objects (or objects that inherited from others), but this would be getting two totally different objects... So if I want to retrieve all the items of a MyNode instance, I don't know what to put in the relationship: class MyNode: [ ... ] def getAllNodes(): items = session.query(?????).all() #What to put in ???? ? "object", maybe? Thank you in advance! -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalch...@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.