I'm experiencing a behavior regarding the way lazy='joined' works on a
JTI setup that is not totally clear to me. I have a basic JTI setup
as follows
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, Column, Integer, String,
ForeignKey
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy.orm.collections import attribute_mapped_collection
engine=create_engine('sqlite://', echo=True)
Base = declarative_base(engine)
def monkey_repr():
def __repr__(self):
"""Show attribute values in the repr()."""
simple_props = ', '.join('%s=%r' % (attr, value)
for attr, value in
sorted(vars(self).items())
if isinstance(value, (basestring,
int)))
return '<%s (%s)>' % (type(self).__name__, simple_props)
return __repr__
Base.__repr__ = monkey_repr()
class Company(Base):
__tablename__ = 'companies'
id = Column(Integer, primary_key=True)
people = relationship('Person', lazy='joined',
collection_class=attribute_mapped_collection('name'))
class Person(Base):
__tablename__ = 'people'
id = Column(Integer, primary_key=True)
_company_id = Column(Integer, ForeignKey('companies.id'))
name = Column(String)
discriminator = Column('type', String(50))
__mapper_args__ = {'polymorphic_on': discriminator}
class Engineer(Person):
__tablename__ = 'engineers'
__mapper_args__ = {'polymorphic_identity': 'engineer'}
name = Column(String)
department = Column(String)
id = Column(Integer, ForeignKey('people.id'), primary_key=True)
primary_language = Column(String(50))
class Accountant(Person):
__tablename__ = 'accountants'
__mapper_args__ = {'polymorphic_identity': 'accountant'}
name = Column(String)
bank = Column(String)
id = Column(Integer, ForeignKey('people.id'), primary_key=True)
primary_language = Column(String(50))
Base.metadata.create_all()
sess = sessionmaker()()
company = Company()
for p in [Engineer(name='far'),
Person(name='nar'),
Engineer(name='zar', department='rover'),
Accountant(name='jak', bank='hsb')]:
company.people.set(p)
sess.add(company)
sess.commit()
sess.expunge_all()
print "\n#\n# Query for the first company \n#\n"
c = sess.query(Company).first()
print c.people
print "\n#\n# This will issue another query even though lazy='joined'
\n#\n"
print c.people['zar'].department
Now, this makes sense if SA is only doing a joinedload on the base
Person model (because thats how the relationship is defined), in
which case I would like to eager load all the data in the models that
extend from Person. Am I missing something simple?
Thanks!
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
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.