in fact its not supposed to be eager loading, since self-referential
eager loads are not supported. set lazy=True to fix this issue.
On Jul 18, 2006, at 7:01 PM, Sol wrote:
> Michael Bayer wrote:
>> for this program, move the "managers" property after the
>> compilation of
>> the other mappers:
>>
>> person_mapper.compile()
>> person_mapper.add_property('managers', relation(Manager,
>> secondary=people_managers, lazy=False))
>>
>> this is a workaround for this particular issue.
>
> For sqlite it works now. But if i choose to use a postgresql
> database i
> will only be able to select employees, others will fail.
>
> e. g.
>
>>>> from polymorphic import *
>>>> session.query(Manager).select()
> ...
> ...
> SQLError: (ProgrammingError) relation "anon_d9b1" does not exist
>
>
> Thanks for your help so far:)
>
> Cheers, Sol.
> from sqlalchemy import *
>
> db = create_engine('sqlite:///sqlite_base')
> #db = create_engine('postgres:///satest')
> metadata = BoundMetaData(db)
>
> session = create_session()
>
> class Employee(object):
> def __init__(self, name):
> self.name = name
> def __repr__(self):
> return self.__class__.__name__ + " " + self.name
>
> class Manager(Employee):
> def __init__(self, name, manager_data):
> self.name = name
> self.manager_data = manager_data
> def __repr__(self):
> return self.__class__.__name__ + " " + self.name + " " +
> self.manager_data
>
> class Engineer(Employee):
> def __init__(self, name, engineer_info):
> self.name = name
> self.engineer_info = engineer_info
> def __repr__(self):
> return self.__class__.__name__ + " " + self.name + " " +
> self.engineer_info
>
>
> people = Table('people', metadata,
> Column('person_id', Integer, primary_key=True),
> Column('name', String(50)),
> Column('type', String(30)))
>
> engineers = Table('engineers', metadata,
> Column('person_id', Integer, ForeignKey('people.person_id'),
> primary_key=True),
> Column('engineer_info', String(50)),
> )
>
> managers = Table('managers', metadata,
> Column('person_id', Integer, ForeignKey('people.person_id'),
> primary_key=True),
> Column('manager_data', String(50)),
> )
>
> people_managers = Table('people_managers', metadata,
> Column('person_id', Integer, ForeignKey("people.person_id")),
> Column('manager_id', Integer, ForeignKey("managers.person_id"))
> )
>
> person_join = polymorphic_union( {
> 'engineer':people.join(engineers),
> 'manager':people.join(managers),
> 'person':people.select(people.c.type=='person'),
> }, None, 'pjoin')
>
>
>
>
> person_mapper = mapper(Employee, people, select_table=person_join,
> polymorphic_on=person_join.c.type, polymorphic_identity='person',
> #properties = dict(managers = relation(Manager,
> secondary=people_managers, lazy=False))
> )
>
>
>
> engineer_mapper = mapper(Engineer, engineers,
> inherits=person_mapper, polymorphic_identity='engineer')
> manager_mapper = mapper(Manager, managers, inherits=person_mapper,
> polymorphic_identity='manager')
>
> person_mapper.compile()
> person_mapper.add_property('managers', relation(Manager,
> secondary=people_managers, lazy=False))
>
> def create_some_employees():
> people.create()
> engineers.create()
> managers.create()
> people_managers.create()
> session.save(Manager('Tom', 'knows how to manage things'))
> session.save(Engineer('Kurt', 'knows how to hack'))
> session.flush()
>
> create_some_employees()
>
-------------------------------------------------------------------------
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
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users