On Sep 19, 2011, at 10:37 AM, Moshe C. wrote:

> Hi,
> I have the following situation:
> There are two tables, A and B , both of which have an "id" column.
> 
> In a certain part of the code I have an ORM query object.
> That query object is returned by some function and I have no apriori
> knowledge of its structure.
> I am assured, though that there will be an 'id' column. It might be a
> SELECT from A, a SELECT from B or a SELECT from A join B  ON
> A.id=B.id.
> 
> I would like to execute a query.values('id').
> 
> This works for the first two cases but in the case of a join it
> complains because it does not know if I mean A.id or B.id.
> The thing is that I obviously do not care because they are both equal
> as a result of the join condition.
> I cannot select A.id or B.id because there is always a case of a query
> that does not have A or B.
> 
> Is there a way to solve this elegantly? I.e. without having to inspect
> that structure of the query first.


Do a hasattr() on both A and B to see which one has "id".     So a bit of 
inspection but not much.  Otherwise, the Query should be constructed such that 
one of the attributes is definitely *named* "id" so that values('id') would 
work.    Using code like MySelectable.somecolumn.label('id') would ensure this.





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