well at the moment this is how it has to work, since youre trying to achieve the polymorphic effect at the level of every mapper....this is not *so* typical of a use case, its a little more ambitious. i dont think its going to reveal too many more issues than a simplistic inheritance relationship would raise.
theres a ticket to get the polymorphic query to be automatically generated, instead of using the "polymorphic union" explicitly, which would probably help. this enhancement relies upon the fact that mappers are compiled after all of them are constructed...a feature which has taken me three minor versions so far to get (close to) right :). hibernate's approach to polymorphism doesnt use UNIONs, it just issues distinct queries for everything. I suppose if i had gone with that, some of these decisions might be easier (or maybe not). apparently JSR-220 (based on hibernate) actually calls for all mappers to be completely polymorphic, such that if you issue an open query on "java.lang.Object", it will load every object of every type in the entire database ! that seems kind of wacky to me, more of an OODB type of thing. On Jul 20, 2006, at 3:38 PM, Sol wrote: > This time with attachement;) >> Same old topic. This time i try to make a some what longer >> polymorphic >> hierarchy, like: >> >> Base <= Item <= Leaf >> >> The attachment is what i came up with. The first impression is >> that it >> works, but my confidence in it is limited. Further more it will >> become >> ugly if we add more hierarchies. >> >> Is there a more simplistic way to achieve this? >> >> Cheers, Sol. > from sqlalchemy import * > > #db = create_engine('postgres:///satest') > db = create_engine("sqlite:///:memory:") > > metadata = BoundMetaData(db) > > > # classes > class Base(object): > def __repr__(self): > return "%s %s" % (self.__class__.__name__, self.id) > > class Item(Base): pass > > class Leaf(Item): pass > > # tables > base = Table('base', metadata, > Column('id', Integer, primary_key = True), > Column('type', String), > ) > > item = Table('item', metadata, > Column('id', Integer, ForeignKey('base.id'), primary_key = True), > ) > > leaf = Table('leaf', metadata, > Column('id', Integer, ForeignKey('item.id'), primary_key = True), > ) > > # mappers > base_join = polymorphic_union( > { > 'leaf' : base.join(item).join(leaf), > 'item' : base.join(item), > 'base' : base.select(base.c.type=='base'), > }, None, 'pjoin') > > > base_mapper = mapper(Base, base, select_table = base_join, > polymorphic_on = base_join.c.type, > polymorphic_identity='base', > ) > > > item_join = polymorphic_union( > { > 'leaf' : base.join(item).join(leaf), > 'item' : base_join.select(base_join.c.type=='item'), > }, None, 'pjoin') > > > item_mapper = mapper(Item, item, select_table = item_join, > polymorphic_on = base_join.c.type, > inherits=base_mapper, > polymorphic_identity='item') > > leaf_mapper = mapper(Leaf, leaf, inherits=item_mapper, > polymorphic_identity='leaf') > > > # create all tables > metadata.create_all() > > # create a session > #metadata.engine.echo = True > session = create_session() > > session.save(Leaf()) > session.save(Leaf()) > session.save(Base()) > session.save(Item()) > session.save(Item()) > session.flush() > ---------------------------------------------------------------------- > --- > Take Surveys. Earn Cash. Influence the Future of IT > Join SourceForge.net's Techsay panel and you'll get the chance to > share your > opinions on IT & business topics through brief surveys -- and earn > cash > http://www.techsay.com/default.php? > page=join.php&p=sourceforge&CID=DEVDEV________________________________ > _______________ > Sqlalchemy-users mailing list > Sqlalchemy-users@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users ------------------------------------------------------------------------- Take Surveys. Earn Cash. Influence the Future of IT Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys -- and earn cash http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV _______________________________________________ Sqlalchemy-users mailing list Sqlalchemy-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users