On Oct 11, 2010, at 2:48 PM, Carl wrote:

> Everyone - TIA for any help.  Here's my situation - I've got a large
> number of different tables that all have the same columns.  I'd like
> to treat these tables as one giant table that I can query against.  I
> thought I could do this by unioning all the tables, making a subquery
> and using the arbitrary "c" attribute, but I'm not successful in the
> slightest.  Here's what I'm trying to do in some pseudo code:
> 
> queries = []
> for table_class in table_classes:
>    q = Session.query(table_class)
>    q = q.filter(<some filters>)
>    queries.append(q)
> 
> q = queries[0].union_all(*queries[1:])
> q = q.subquery
> 
> alias1 = aliased(some_table_1)
> 
> uber_query = Session.query( alias1.col1, alias1.col2, q.c.col1,
> q.c.col2)
> 
> 
> The idea is that the tables that all have the same columns will be
> represented by 'col1' and so on.  But, well, this isn't working.  I
> realize my approach may not be correct, I'll take any help I can
> get.

The aliased() construct will always have the same attributes as the mapped 
entity which its aliasing (and in that sense your example above is nonsensical, 
if "some_table_1" is not a mapped class).   So the usage of aliased() implies 
that you're creating a mapper against your union first with a new class, which 
is a little old school and I wouldn't bother doing it that way.

You'd get better control over the columns within a union if you work with Table 
objects directly and the "union()" function.    You'd then have a construct 
who's .c. attribute will have the names that are present in the topmost 
selectable of the union().    


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