I have a one to one relationship between Company and Employee where
Employee is subclassed using joined table inheritance into Manager and
Engineer. I'm trying to query for a list of companies with the
employee eagerloaded including the type-specific columns.

I've been looking at the example at
http://www.sqlalchemy.org/docs/mappers.html#creating-joins-to-specific-subtypes,
but I can't figure out how to construct a query to eagerload the
attributes onto the employee on the company.

Here's what I have at the moment:

companies = Session.query(Company) \
    .join((
        Employee.__table__ \
            .outerjoin(Engineer.__table__) \
            .outerjoin(Manager.__table__),
        Company.employee
    )) \
    .all()

which generates

SELECT <company columns>
FROM companies INNER JOIN (
    SELECT <employee columns>, <engineer columns>, <manager columns>
    FROM employees
    LEFT OUTER JOIN engineers ON employees.id = engineers.id
    LEFT OUTER JOIN managers ON employees.id = managers.id
) AS anon_1 ON anon_1.employees_id = companies.employee_id

So I guess I need to get the Engineer and Manager columns into the
field list and then for them to be populated on the Company.employee
relationship - I'm struggling with this. Is there such as thing as
with_polymorphic() for joinedload()?

My next question is about how to manage this sort of query strategy in
the long term when the total number of subtypes might get larger (e.g.
50 types of employees). The number of types per company would remain
low (e.g. a TechCompany might have Engineers and Managers, but not
Designers or Salesmen). Should I somehow store the types of employee
to query polymorphically for on a per-Company basis?

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to