Hi, I have two connected problems.

First one is in subquery:

query = db.session.query(OrderColorDistribution, Order, Article) \
    .filter(OrderColorDistribution.color_id == color_id) \
    .join(Order, Order.order_id == OrderColorDistribution.order_id) \
    .join(Article, and_(Article.ax_item_id == Order.ax_item_id, 
Article.group_id == group_id))
return query.subquery('ocr_subquery')


both OrderColorDistribution and Order contains order_id. So it returns 
query:
SELECT order_color_distribution.order_id AS order_id, ... orders.order_id 
AS order_id,

My question is - how to relabel AS to have table prefix for all fields from 
this table?
To get something like this: order_color_distribution.order_id AS 
order_color_distribution_order_id and orders.order_id AS orders_order_id

And now main question:
In the main query I join this subquery:

query = query.outerjoin(ocr_subquery, and_(ocr_subquery.c.date == 
PlanMonthly.date,
                                           ocr_subquery.c.color_id == 
PlanMonthly.color_id,
                                           ocr_subquery.c.group_id == 
PlanMonthly.group_id))
query = query.options(contains_eager(PlanMonthly.orders_by_color)
                      .contains_eager(OrderColorDistribution.order))


Each PlanMonthly contains multiple OrderColorDistributions, and each 
OrderColorDistribution contain one order.
I would like to have them all loaded at once, as I have this data in a 
query.

But - without alias contains_eager doesn't know where to get data from. 
(With alias it adds additional FROM, which is completely incorrect).
I need to make:
1. subquery use table prefix for all fields (like label for single field) 
to have orders.order_id AS orders_order_id
2. make first contains_eager use label (like alias, but without FROM 
modifications) ocr_subquery.order_color_distributions_<column>
3. make second contains_eager use label ocr_subquery.orders_<column>

Is it possible?

Many thanks.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to