elplatt
elplatt

Reputation: 3387

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)        

Upvotes: 2

Views: 694

Answers (1)

univerio
univerio

Reputation: 20518

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

Upvotes: 3

Related Questions