Skip to content

sa.union_all() fails with sa.text() if use ORDER BY. #6434

Discussion options

You must be logged in to vote

Hi,

SQLAlchemy does not parses the content of the text, so if the query requires parenthesis they are needed in the text, e.g. sa.text(f'''(SELECT run_id FROM {view_name})''').

An alternative is to use the lowercase column and table to generate the queries:

        for bu in bu_ids:
            view_name = f'view_{bu}'
            queries.append(
                sa.select([sa.column('run_id')])
                .select_from(sa.table(view_name))
                .order_by(sa.column(run_id))
                .limit(1)
            )
        q_union = sa.union_all(*queries)
        result = conn.execute(q_union).fetchall()

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by zzzeek
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #6385 on May 06, 2021 14:25.