On Mar 9, 2009, at 5:05 PM, Bryan wrote:

>
> Yes, Time.emp is a relation to the Employee object.  I was trying to
> avoid having this query know anything about the actual table column
> names, so I wanted to avoid Time.employeeId.  When I say Time.emp,
> howcome all of the table's columns are added to the query output?

every InstrumentedAttribute is associated with a MapperProperty which  
handles the details of interfacing the attribute to a relational  
concept like a Table.    Different MapperProperty classes handle  
different types of relational concepts - the ColumnProperty handles  
the relation to a single column, and the RelationProperty to a related  
class/selectable.   Each MP communicates at the class level (i.e. when  
you say MyClass.foo==bar) to the comparison methods on  
InstrumentedAttribute via a Comparator object, which calls upon the  
__clause_element__() method of the MP in order to get at a SQL-level  
construct.   For ColumnProperty, its Column, for RelationProperty, its  
usually Table or Alias (which in turn references a Select, usually).    
Query() basically looks for __clause_element__() and renders it.

there have been proposals both to have query(Class.somerelation) raise  
an error, since what it should do is ambiguous, or to render the  
SomeClass which it represents, either with or without the implicit  
join.  Its the implicit join part that is ambiguous.   It would be an  
implicit behavior which is redundant against functionality which is  
already available and widely documented.   but to not do the implicit  
join seems inconsistent in that when you say SomeClass.foo, it implies  
the "foo" is associated with the SomeClass the way a column is, unlike  
if you just said query(ParentClass, ChildClass).

today I've been thinking about doing the implicit join but other days  
I think its a bad idea.


>
>
> On Mar 9, 2:02 pm, "Michael Bayer" <mike...@zzzcomputing.com> wrote:
>> Bryan wrote:
>>
>>> The join works great now.  Thanks.
>>
>>> This query is actually being used for a subquery.  Table 'Time' also
>>> has a column 'employeeId', which translates to an orm attribute of
>>> 'emp'.  When I add Time.emp to the columns of this subquery, all
>>> columns of the 'Time' table are output instead of just the
>>> 'employeeId' column.
>>
>>> When I say:
>>> q = orm.query(Time.emp)
>>> print q
>>
>>> A large SQL statement with all the columns in 'Time' appears.  I  
>>> would
>>> like it to just show the Time.employee_time column.  Is this  
>>> possible
>>> using only the orm library, or must I use the tables underlying
>>> columnn objects?
>>
>> do you mean that Time.emp is a relation() to the Employee object ?   
>> if you
>> want just employeeId, ask for that:  query(Time.employeeId)
>>
>>
>>
>>> On Mar 9, 1:12 pm, "Michael Bayer" <mike...@zzzcomputing.com> wrote:
>>>> specify the join as an "on" condition:
>>
>>>> q.join(Time.account)
>>>> q.join(Time.job)
>>
>>>> Bryan wrote:
>>
>>>>> I have a table 'Time' that has many-to-1 relationships to tables  
>>>>> 'Job'
>>>>> and 'Account'.  There is also a 1-to-many relationship between  
>>>>> job and
>>>>> Account.
>>
>>>>> Tables
>>>>> ----------------------------------------------
>>>>> Time
>>>>>    --> Account
>>>>>    --> Job
>>>>> Job --> Account
>>
>>>>> I am trying to pull back a few rows from Time, Job and Account  
>>>>> via the
>>>>> orm library.  I want to try and avoid using the actual sql  
>>>>> objects.
>>
>>>>> I can't join them simply like below because the reference  
>>>>> between Job
>>>>> and Account is making the join ambiguous.  I want to join Time  
>>>>> to Job
>>>>> and Time to Account.
>>
>>>>> q = orm.query(
>>>>>       func.sum(Time.hours),
>>>>>       Time.day,
>>>>>       Job.number,
>>>>>       Account.code
>>>>>      )
>>>>> q = q.join(Job)
>>>>> q = q.join(Account)
>>
>>>>> How can i tell sqlalchemy to join Job and Account to Time, and  
>>>>> not to
>>>>> eachother?
>>
>>>>> Bryan
>>
>>
> >


--~--~---------~--~----~------------~-------~--~----~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to