On May 3, 2012, at 8:15 PM, Michael Bayer wrote:
> 1. put the tablename qualification back on:
>
> sq = s.query(BasePrAc).with_labels().subquery()
>
> But then explicit reference to the columns needs to include the tablename:
>
> q = s.query(Post).join((sq, sq.c.base_products_accessories_id ==
> Post.basePrAcId)).all()
>
> Though in this case you can just join using the relationship:
>
> q = s.query(Post).join((sq, Post.basePrAc)).all()
>
> 2. build a join using fold_equivalents:
>
> This is something that could theoretically make it's way into Query with some
> complexity. join() specifically supports "folding" the equivalently named
> and FK-linked columns. It's a little tricky to get it into the mapped join
> but you can do it by hand:
>
> sq =
> BasePrAc.__mapper__.mapped_table.select(fold_equivalents=True).alias()
>
> above, fold_equivalents is only accepted by the select() that comes from a
> Join object, which in this case BasePrAc.__mapper__.mapped_table is.
OK, here is better than using fold_equivalents. This might make it into 0.8 in
some form:
from sqlalchemy.sql.util import reduce_columns
def reduced(query):
stmt = query.statement
return stmt.with_only_columns(reduce_columns(stmt.inner_columns))
sq = reduced(s.query(BasePrAc)).alias()
q = s.query(Post).join((sq, sq.c.id == Post.basePrAcId)).all()
print q
reduce_columns() will limit the set of columns in a list of columns to those
that are "equivalent" based on foreign key.
--
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.