On Dec 23, 2010, at 10:29 PM, kindly wrote:

> Oh I see.
> 
> The issue was that the query was trying to filter on the parent. i.e
> 
> query(ManagerInfo).join("manager").filter(Employee.foo == 'bar')
> 
> This makes a cross join.
> 
> The use case is that this point in the application the ManigerInfo did
> not know what it had joined to. All it knew was that it joined using
> "manager" and Employee was the superclass.  This may sound silly on
> this small example.
> 
> I already found you can get out the class by finding it in the
> descriptor i.e  ManagerInfo.manager.property.mapper.class_,   however
> I am not sure how robust that is.  It will be fine for now.

sure.  In 0.7 it looks like we have ManagerInfo.manager.class_ too.


> 
> 
> On Dec 24, 1:53 am, Michael Bayer <[email protected]> wrote:
>> On Dec 23, 2010, at 8:38 PM, kindly wrote:
>> 
>> 
>> 
>>> Hello
>> 
>>> Is there a nice way to get the subquery object that relates to anon_1
>>> in the below case so you can use it to filter on?
>> 
>>> You can get it by q._joinpoint['_joinpoint_entity'] but that is
>>> clearly private.
>> 
>>> Thanks
>> 
>>> David
>> 
>>> class Employee(object):    pass
>> 
>>> class Manager(Employee):    pass
>> 
>>> class ManagerInfo(object):    pass
>> 
>>> employees = Table('employees', metadata,
>>>   Column('employee_id', Integer, primary_key=True),
>>>   Column('type', String(30), nullable=False)
>>> )
>> 
>>> managers = Table('managers', metadata,
>>>   Column('employee_id', Integer, ForeignKey('employees.employee_id'),
>>> primary_key=True),
>>>   Column('manager_info_id', Integer, ForeignKey('manager_info.id'),
>>> primary_key=True),
>>> )
>> 
>>> manager_info = Table('manager_info', metadata,
>>>   Column('id', Integer, primary_key=True),
>>>   Column('data', String(30), primary_key=True),
>>> )
>> 
>>> mapper(Employee, employees, polymorphic_on=employees.c.type, \
>>>    polymorphic_identity='employee')
>> 
>>> mapper(Manager, managers, inherits=Employee,
>>> polymorphic_identity='engineer')
>> 
>>> mapper(ManagerInfo,
>>>       manager_info,
>>>       properties = dict(manager = orm.relation(Manager))
>>>      )
>> 
>>> session = Session()
>> 
>>> q = session.query(ManagerInfo).join("manager")
>> 
>>> print q
>> 
>>> """SELECT manager_info.id AS manager_info_id, manager_info.data AS
>>> manager_info_data
>>> FROM manager_info JOIN (SELECT employees.employee_id AS
>>> employees_employee_id, employees.type AS employees_type,
>>> managers.employee_id AS managers_employee_id, managers.manager_info_id
>>> AS managers_manager_info_id
>>> FROM employees JOIN managers ON employees.employee_id =
>>> managers.employee_id) AS anon_1 ON manager_info.id =
>>> anon_1.managers_manager_info_id"""
>> 
>> Once you join to ManagerInfo.manager, you'd reference the right joinpoint 
>> using the Manager entity itself:
>> 
>>         query(ManagerInfo).join("manager").filter(Manager.foo == 'bar')
>> 
>> That's the reason for the whole joinpoint entity stuff, the Query is keeping 
>> track of it, will recognize the usage of "Manager" and will alias all 
>> expressions that use it.
>> 
>> You can also join to an alias of Manager:
>> 
>>         malias = aliased(Manager)
>>         query(ManagerInfo).join((malias, 
>> ManagerInfo.manager)).filter(malias.foo == 'bar')
>> 
>> If you need to reference the individual tables that comprise Manager, 
>> usually you do that via referencing "managers" and "employees" directly to 
>> start with.
>> 
>> The joined table inheritance query stuff has taken several years to get 
>> right, so if there's something its not doing at that level let me know and I 
>> can suggest a workaround or provide a fix.
> 
> -- 
> 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.
> 

-- 
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.

Reply via email to