Reputation: 95
I'm using from_statement() function do complex queries that requires multiple subtables which works fine apart from one thing, that you have to specify column names in query() calls.
e.g. If you want to get columns A and B from the following pseudo query
SELECT t1.colA AS A, t2.colB AS B FROM
(
SELECT
sth AS colA
FROM
(
...
) t1
...
then you'll have to specify A and B in your query()
session.query('A', 'B').from_statement(above_statement).all()
which can get annoying when you want to get say more than 5 columns from your query.
Is there some way to just make SQLAlchemy return whatever it gets from the query?
I've tried query('*')
or just query()
but neither works.
Upvotes: 2
Views: 1479
Reputation: 39830
I think the easiest way would be to use execute()
:
session.execute(above_statement).fetchall()
If you still want to use query()
, then you can probably put all of your columns into a list and then unpack them:
cols = ['A', 'B']
session.query(*cols).from_statement(above_statement).all()
Upvotes: 2