this relation will require you to configure primaryjoin and secondaryjoin (it should be raising an error without them). an example is attached.
--~--~---------~--~----~------------~-------~--~----~ 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 * engine = create_engine('sqlite://', echo=True) metadata = MetaData(engine) class Person(object): def __init__(self, name): self.name = name people = Table('people', metadata, Column('id', Integer, primary_key=True), Column('name', String(50))) managers = Table('managers', metadata, Column('person_id', Integer, ForeignKey('people.id')), Column('manager_id', Integer, ForeignKey('people.id'))) metadata.create_all() mapper(Person, people, properties={ 'manager':relation(Person, secondary=managers, primaryjoin=people.c.id==managers.c.person_id, secondaryjoin=people.c.id==managers.c.manager_id, uselist=False, backref=backref('direct_reports', primaryjoin=people.c.id==managers.c.manager_id, # the redundant 'primaryjoin' secondaryjoin=people.c.id==managers.c.person_id, # and 'secondaryjoin' are not needed in 0.5 collection_class=set ) ) }) sess = sessionmaker(transactional=True)() p1 = Person('p1') p2 = Person('p2') p3 = Person('p3') m1 = Person('m1') m2 = Person('m2') p1.manager = m1 p2.manager = m1 p3.manager = m2 m2.direct_reports.add(m1) sess.save(m2) sess.commit() sess.clear() [m1, m2] = sess.query(Person).filter(Person.name.like('m%')).order_by(Person.name).all() [p1, p2, p3] = sess.query(Person).filter(Person.name.like('p%')).order_by(Person.name).all() assert m2.direct_reports == set([p3, m1]) assert m1.manager == m2 assert m1.direct_reports == set([p1, p2])
On Jun 27, 2008, at 5:30 PM, Tom Hogarty wrote: > > Hello, > > I have the following scenario where I want the same class/table on > both sides of a relation. > > person table > - id > - name > > manager table > - person_id > - manager_id > > I define both tables and a Person class, then create a relation in the > person mapper like: > 'manager' : relation(Person, secondary=managers, > backref='direct_reports') > > When I do this, the 'manager' attribute doesn't show up in Person > objects when I query on people. The error I get is: > AttributeError: 'Person' object has no attribute 'manager' > # query is something like > session.query(Person).filter_by(name='Joe').one() > > Any hints on how I can do this. I have other basic relations working > (1-1, 1-M, M-1) but they all have different classes/tables on each end > of the relation. > > Regards, > > -Tom > --~--~---------~--~----~------------~-------~--~----~ > 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 > -~----------~----~----~----~------~----~------~--~--- >