Hi Michael and all.... I successfully built a "all nodes" relationship following your guidelines.
As: subq1=select([caso_vinculo.c.caso_1_id.label('id1'),caso_vinculo.c.caso_2_id.label('id2')]).union(select([caso_vinculo.c.caso_2_id, caso_vinculo.c.caso_1_id])) subq2=aliased(subq1) CasoMapper = mapper(Caso, caso, properties= { 'LinkedNodes':relation(Caso, secondary=subq2, primaryjoin=caso.c.id == subq2.c.id1, secondaryjoin=subq2.c.id2==caso.c.id), So with this property I can get both left and right linked nodes. with: CasoAlias=aliased(Caso) q1=session.query(Caso.id).outerjoin(CasoAlias.LinkedNodes) q1.all() works fine and once I add a new column from the aliased entity (CasoAlias) q2=q1.add_column(CasoAlias.id) it compiles fine SELECT caso.id AS caso_id, caso_1.id AS caso_1_id FROM caso AS caso_1 LEFT OUTER JOIN (SELECT caso_vinculo.caso_1_id AS id1, caso_vinculo.caso_2_id AS id2 FROM caso_vinculo UNION SELECT caso_vinculo.caso_2_id AS caso_2_id, caso_vinculo.caso_1_id AS caso_1_id FROM caso_vinculo) AS anon_1 ON caso_1.id = anon_1.id1 LEFT OUTER JOIN caso ON anon_1.id2 = caso.id BUT q2.all() breaks with Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> q2.all() File "build\bdist.win32\egg\sqlalchemy\orm\query.py", line 2115, in all return list(self) File "build\bdist.win32\egg\sqlalchemy\orm\query.py", line 2351, in instances labels) for row in fetch] File "build\bdist.win32\egg\sqlalchemy\orm\query.py", line 3342, in proc return row[column] File "build\bdist.win32\egg\sqlalchemy\engine\base.py", line 2616, in __getitem__ processor, obj, index = self._parent._key_fallback(key) File "build\bdist.win32\egg\sqlalchemy\engine\base.py", line 2835, in _key_fallback expression._string_or_unprintable(key)) NoSuchColumnError: "Could not locate column in row for column '%(34374288 caso)s.id'" I am using sa '0.7.8' Any lead on this error? Thanks in advance for any help!! Adolfo On Monday, August 20, 2012 5:07:39 PM UTC-5, adolfo wrote: > > I have a Self-Referential Many-to-Many Relationship situation where the > > 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" > > > works fine. > The problem: > I need a "related nodes" relationship, which, in one expression, returns > all related nodes, both left nodes and right nodes, excluding the given > node itself. > Is that possible using the RELATIONSHIP construct? > > Adolfo > > > -- 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/-/uGFItXQmwU4J. 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.