Hey guys, hope I can get an answer to my question here. In hindsight, r/Flask was probably not the best place to post to get SQLAlchemy asnwers.
Here's the thread that I posted yesterday: http://www.reddit.com/r/flask/comments/1pac6v/selfreferential_manytomany_relationship_two_quick/ Here it is copy and pasted. *Hey Flaskers, trying to wrap my head around some SQLAlchemy magic for my app. The SQLAlchemy docs and SO helped me understand how to implement a self-referential many-to-many relationship, but I'm left with two lingering questions.* *Here's the code:* *node_to_node = Table("node_to_node", Column("left_node_id", db.Integer, ForeignKey("node.id"), primary_key=True), Column("right_node_id", db.Integer, ForeignKey("node.id"), primary_key=True) ) class Node(db.Model): id = db.Column(db.Integer, primary_key=True) right_nodes = relationship("Node", secondary=node_to_node, primaryjoin=(id==node_to_node.c.left_node_id), secondaryjoin=(id==node_to_node.c.right_node_id), backref="left_nodes", lazy = 'dynamic' ) def add_node(self, node): self.right_nodes.append(node)* 1. *How does my add_node function know to put the self node as the "left_node_id" and the node argument as my "right_node_id"? It looks like primaryjoin and secondaryjoin create custom join conditions, but they don't differentiate which id they take.* 2. *Why is it that when I query this relationship, I get the right nodes, not the left? I've seen this left/right paradigm in the SQLAlchemy docs for quite some time, but it still confuses me.* *Any and all answers would be very much appreciated! It nags me that I can't seem to fully get this subject, and I'd like to understand before I move on. Gracias!* *Flash edit: This is code originally from the SQLAlchemy docs, converted to Flask appropriate code. There may be some errors, but the general sense of things should be sound.* * * rd4, one of the responders in the Reddit thread, gave me a decent idea of what's going on with this code, but I'm still hungry for a more complete answer. The key thing I want to learn is the order of events that take place in this code, because it seems like I have some misconceptions about how the relationship() function actually works. I'll be checking this thread and the Reddit thread throughout the day - if you've got some extra time, please consider answering! -- 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 [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.
