SELECT * in SQLAlchemy?

Viewed 118682

Is it possible to do SELECT * in SQLAlchemy?

Specifically, SELECT * WHERE foo=1?

11 Answers

Is no one feeling the ORM love of SQLAlchemy today? The presented answers correctly describe the lower-level interface that SQLAlchemy provides. Just for completeness, this is the more-likely (for me) real-world situation where you have a session instance and a User class that is ORM mapped to the users table.

for user in session.query(User).filter_by(name='jack'):
     print(user)
     # ...

And this does an explicit select on all columns.

If you don't list any columns, you get all of them.

query = users.select()
query = query.where(users.c.name=='jack')
result = conn.execute(query)
for row in result:
    print row

Should work.

Where Bar is the class mapped to your table and session is your sa session:

bars = session.query(Bar).filter(Bar.foo == 1)

Turns out you can do:

sa.select('*', ...)

I had the same issue, I was trying to get all columns from a table as a list instead of getting ORM objects back. So that I can convert that list to pandas dataframe and display.

What works is to use .c on a subquery or cte as follows:

U = select(User).cte('U')
stmt = select(*U.c)
rows = session.execute(stmt)

Then you get a list of tuples with each column.

Another option is to use __table__.columns in the same way:

stmt = select(*User.__table__.columns)
rows = session.execute(stmt)

In case you want to convert the results to dataframe here is the one liner:

pd.DataFrame.from_records(rows, columns=rows.keys())

I had the same issue, I was trying to get all columns from a table as a list instead of getting ORM objects back. So that I can convert that list to pandas dataframe and display.

What works is to use .c on a subquery or cte as follows:

U = select(User).cte('U')
stmt = select(*U.c)
rows = session.execute(stmt)

Then you get a list of tuples with each column.

Another option is to use __table__.columns in the same way:

stmt = select(*User.__table__.columns)
rows = session.execute(stmt)

In case you want to convert the results to dataframe here is the one liner:

pd.DataFrame.from_records(dict(zip(r.keys(), r)) for r in rows)
every_column = User.__table__.columns
records = session.query(*every_column).filter(User.foo==1).all()

When a ORM class is passed to the query function, e.g. query(User), the result will be composed of ORM instances. In the majority of cases, this is what the dev wants and will be easiest to deal with--demonstrated by the popularity of the answer above that corresponds to this approach.

In some cases, devs may instead want an iterable sequence of values. In these cases, one can pass the list of desired column objects to query(). This answer shows how to pass the entire list of columns without hardcoding them, while still working with SQLAlchemy at the ORM layer.

Related