From: Mike Bayer Date: Tue, 11 Mar 2014 21:10:16 +0000 (-0400) Subject: add a string example for self-refenretial many-to-many X-Git-Tag: rel_0_9_4~68 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=50f0d9be33d1d802c8642e9a338905f6ae396cb4;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git add a string example for self-refenretial many-to-many --- diff --git a/doc/build/orm/relationships.rst b/doc/build/orm/relationships.rst index 189dbdd925..d156b4a0ef 100644 --- a/doc/build/orm/relationships.rst +++ b/doc/build/orm/relationships.rst @@ -1195,6 +1195,24 @@ In the Declarative form above, as we are declaring these conditions within the P block that corresponds to the ``Node`` class, the ``id`` variable is available directly as the :class:`.Column` object we wish to join with. +Alternatively, we can define the :paramref:`~.relationship.primaryjoin` +and :paramref:`~.relationship.secondaryjoin` arguments using strings, which is suitable +in the case that our configuration does not have either the ``Node.id`` column +object available yet or the ``node_to_node`` table perhaps isn't yet available. +When referring to a plain :class:`.Table` object in a declarative string, we +use the string name of the table as it is present in the :class:`.MetaData`:: + + class Node(Base): + __tablename__ = 'node' + id = Column(Integer, primary_key=True) + label = Column(String) + right_nodes = relationship("Node", + secondary="node_to_node", + primaryjoin="Node.id==node_to_node.c.left_node_id", + secondaryjoin="Node.id==node_to_node.c.right_node_id", + backref="left_nodes" + ) + A classical mapping situation here is similar, where ``node_to_node`` can be joined to ``node.c.id``::