简体   繁体   中英

SQLAlchemy expression language: how to join table with subquery?

I have a subquery table inner_stmt , which I want to join with a table revisions . But revisions.join() gives the following error:

Neither 'Label' object nor 'Comparator' object has an attribute 'c'

Here is my code. What am I doing wrong?

inner_stmt = select([
        ratings.c.article_name,
        func.min(ratings.c.timestamp).label('mintime')]) \
    .group_by(ratings.c.article_name).label('firstga')

stmt = select([
        revisions.c.article_id,
        func.max(revisions.c.length_bytes)]) \
    .select_from(revisions.join(
        inner_stmt,
        revisions.c.article_name == inner_stmt.c.article_name)) \
    .group_by(table.c.article_id)        

You are label ing your subquery inner_stmt . label is for columns or expressions, ie SELECT ... AS ... . You want alias instead, which is for subquery expressions, ie FROM ... AS ... . You cannot access columns ( .c.<name> ) from the former, even in SQL, because it's a SQL expression.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM