Something like this? The association table is declared in the relationships,
but never referenced when creating or accessing objects.
class Assoc(Base):
__tablename__ = 'assoc'
parent = Column(Integer, ForeignKey('m_to_n.id'), primary_key=True)
child = Column(Integer, ForeignKey('m_to_n.id'), primary_key=True)
class MToN(Base):
__tablename__ = 'm_to_n'
id = Column(Integer, primary_key=True)
name = Column(String)
children = relation('MToN', secondary=Assoc.__table__,
primaryjoin='MToN.id==Assoc.parent',
secondaryjoin='MToN.id==Assoc.child',
backref=backref('parents')
)
def __repr__(self):
return "<M:N %s %s>" % (self.id, self.name)
metadata.create_all()
compile_mappers()
p1 = MToN(name='P1')
p2 = MToN(name='P2')
p3 = MToN(name='P3')
c1 = MToN(name='C1')
c1a = MToN(name='C1A')
c2 = MToN(name='C2')
c3 = MToN(name='C3')
p1.children.append(c1)
p1.children.append(c1a)
c1.children.append(c2)
p2.children.append(c1)
c3.parents.append(p1)
c3.parents.append(p3)
session.add_all([p1, p2, p3])
session.commit()
engine.echo=False
qry_p = session.query(MToN).filter(MToN.name.like('P%'))
for p in qry_p:
print '======'
print p
for ch1 in p.children:
print ' ', ch1
for ch2 in ch1.children:
print ' ',ch2
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---