On 8/21/07, Michael Bayer <[EMAIL PROTECTED]> wrote:

> On Aug 21, 2007, at 6:10 PM, Patrick Wagstrom wrote:
> > Unfortunately, when creating the mapper, I can't specify an
> > onclause to the mapper.  Any help on how I would accomplish this
> > and get my such a structure in SQLAlchemy?
> >
>
> the onclause for a joined table inheritance join is specified using
> the "inherit_condition" option on the inheriting mapper().



Thanks for the really quick response to my question.  For the sake of
completeness and to help anyone else who may come across this problem, I've
attached a working copy of the generic graph structure to this file.  I've
test it with an expanded setup which easily handles multiple levels of
inheritance.

Hope this can help someone else too.

--Patrick

--~--~---------~--~----~------------~-------~--~----~
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 *
from sqlalchemy.orm import *
metadata = MetaData('sqlite://')

element_table = Table('element', metadata,
    Column('id', Integer, primary_key=True, default=None),
    Column('type', CHAR(1)),
)

link_table = Table('link', metadata,
    Column('id', Integer, ForeignKey('element.id'), primary_key=True),
    Column('source_id', Integer, ForeignKey('element.id')),
    Column('target_id', Integer, ForeignKey('element.id')),
)

metadata.create_all()

element_join = element_table.outerjoin(link_table, onclause=link_table.c.id==element_table.c.id)

class Element(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 Link(Element):pass

element_mapper = mapper(Element, element_table, select_table=element_join, polymorphic_on=element_table.c.type, polymorphic_identity='e')
element_mapper.compile()

link_mapper = mapper(Link, link_table, inherits=element_mapper, inherit_condition=link_table.c.id==element_table.c.id, polymorphic_identity='l',
                     properties={'source': relation(Element, primaryjoin=element_table.c.id==link_table.c.source_id, uselist=False),
                                 'target': relation(Element, primaryjoin=element_table.c.id==link_table.c.target_id, uselist=False)})
link_mapper.compile()

sess = create_session()
e1 = Element()
e2 = Element()
l = Link(source=e1, target=e2)
l2 = Link(source=e1, target=l)

for o in [e1, e2, l, l2]:
    sess.save(o)
sess.flush()
sess.clear()

print sess.query(Element).list()
print sess.query(Link).list()

Reply via email to