I know ive put many folks through the wringer dealing with this function, in order to get their inherited mappers to load instances polymorphically. It seems like we probably dont need it for the most common case, which is that you are using only joined-table inheritance from a single common base table (i.e. no concrete- inheritance), and have a specific column in your base table that indicates the "polymorphic type". if thats the case, you can forego the usage of polymorphic_union and just string together your tables using joins and outerjoins. Theres a slight tweak in the post 0.3.8 trunk with eager loading to get "eager loading to a polymorphic mapper" to work with this case.
meaning, you can make mappers like this: page_join = page_table.outerjoin(magazine_page_table).outerjoin (classified_page_table) magazine_join = page_table.join(magazine_page_table).outerjoin (classified_page_table) page_mapper = mapper(Page, page_table, select_table=page_join, polymorphic_on=page_table.c.type, polymorphic_identity='p') magazine_page_mapper = mapper(MagazinePage, magazine_page_table, select_table=magazine_join, inherits=page_mapper, polymorphic_identity='m') classified_page_mapper = mapper(ClassifiedPage, classified_page_table, inherits=magazine_page_mapper, polymorphic_identity='c') full example attached. conclusions we can draw from this ? I am really, really dumb for not seeing this for like, the past year. the queries are a lot easier to create and read, plus we avoid all the other issues UNIONS bring forth (like same column name but different types colliding, etc). --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---
from sqlalchemy import * metadata = BoundMetaData('sqlite://', echo=True) page_table = Table('page', metadata, Column('id', Integer, primary_key=True, default=None), Column('page_no', Integer), Column('type', CHAR(1)), ) magazine_page_table = Table('magazine_page', metadata, Column('page_id', Integer, ForeignKey('page.id'), primary_key=True), Column('orders', String(45)), ) classified_page_table = Table('classified_page', metadata, Column('magazine_page_id', Integer, ForeignKey('magazine_page.page_id'), primary_key=True), Column('titles', String(45)), ) metadata.create_all() page_join = page_table.outerjoin(magazine_page_table).outerjoin(classified_page_table) magazine_join = page_table.join(magazine_page_table).outerjoin(classified_page_table) class Page(object): def __init__(self, **kwargs): for key, value in kwargs.iteritems(): setattr(self, key, value) def __repr__(self): return "%s(%s)" % (self.__class__.__name__, ','.join(["%s=%s" % (k, repr(v)) for k, v in self.__dict__.iteritems() if k[0] != '_'])) class MagazinePage(Page):pass class ClassifiedPage(MagazinePage):pass page_mapper = mapper(Page, page_table, select_table=page_join, polymorphic_on=page_table.c.type, polymorphic_identity='p') magazine_page_mapper = mapper(MagazinePage, magazine_page_table, select_table=magazine_join, inherits=page_mapper, polymorphic_identity='m') classified_page_mapper = mapper(ClassifiedPage, classified_page_table, inherits=magazine_page_mapper, polymorphic_identity='c') sess = create_session() objs = [Page(page_no=5), MagazinePage(page_no=6, orders='some text'), ClassifiedPage(page_no=7, orders='some other text', titles='classified titles')] for o in objs: sess.save(o) sess.flush() sess.clear() print sess.query(Page).list() print sess.query(MagazinePage).list()