thought i'd post the code I came up with for reference: from sqlalchemy import * from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import relationship, sessionmaker from sqlalchemy.ext.associationproxy import association_proxy
Base = declarative_base() def _create_inheritance(supertype, subtype): return Inheritance(supertype, subtype) inheritance_table = Table('inheritance', Base.metadata, Column('sub_name', String(50), ForeignKey('types.name'), primary_key=True), Column('super_name', String(50), ForeignKey('types.name'), primary_key=True)) class Type(Base): __tablename__ = 'types' name = Column(String(50), primary_key=True) abstract = Column(Boolean) top = Column(Boolean) subtypes = relationship('Type', secondary=inheritance_table, primaryjoin=inheritance_table.c.super_name==name, secondaryjoin=inheritance_table.c.sub_name==name, backref='supertypes') def hasSuper(self): return self.supertypes.length > 0 def hasSub(self): return self.subtypes.length > 0 def isAnySubOf(self, tp): #to check for cyclic inheritance if self == tp: return True for typ in self.supertypes: if typ.isAnySubOf(tp): return True return False def isAnySuperOf(self, tp): return tp.isAnySubOf(self) def addSub(self, tp): #some types cannot have supertypes: if not tp.top: #to check for cyclic inheritance: if not self.isAnySubOf(tp): self.subtypes.append(tp) else: raise Exception("cyclic inheritance") else: raise Exception(str(tp) + " cannot have supertype") def addSuper(self, tp): tp.addSub(self) def __init__(self, name, top = False, abstract = False): self.name = name self.top = top self.abstract = abstract def __repr__(self): return "Type(%r)" % (self.name) engine = create_engine('sqlite:///:memory:', echo=False) Base.metadata.create_all(engine) if __name__ == "__main__": Session = sessionmaker(bind=engine) session = Session() try: c1 = Type("A") c2 = Type("B") c3 = Type("C") c1.addSub(c2) c3.addSuper(c1) c3.addSuper(c2) c3.addSub(c1) #<= would be cyclic print "c1.subs" for c in c1.subtypes: print " " + str(c) print "c1.supers" for c in c1.supertypes: print " " +str(c) print "c2.subs" for c in c2.subtypes: print " " + str(c) print "c2.supers" for c in c2.supertypes: print " " + str(c) print "c3.subs" for c in c3.subtypes: print " " + str(c) print "c3.supers" for c in c3.supertypes: print " " + str(c) except Exception, e: print "error: " + str(e) -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. 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.