Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT * in SQLAlchemy?

Is it possible to do SELECT * in SQLAlchemy?

Specifically, SELECT * WHERE foo=1?

like image 614
mike Avatar asked Mar 11 '09 21:03

mike


People also ask

How do I select in SQLAlchemy?

The select() method of table object enables us to construct SELECT expression. The resultant variable is an equivalent of cursor in DBAPI. We can now fetch records using fetchone() method. Here, we have to note that select object can also be obtained by select() function in sqlalchemy.

How does the querying work with SQLAlchemy?

Python Flask and SQLAlchemy ORM All SELECT statements generated by SQLAlchemy ORM are constructed by Query object. It provides a generative interface, hence successive calls return a new Query object, a copy of the former with additional criteria and options associated with it.

What is lazy SQLAlchemy?

Lazy loading refers to objects are returned from a query without the related objects loaded at first. When the given collection or reference is first accessed on a particular object, an additional SELECT statement is emitted such that the requested collection is loaded.


2 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.

like image 199
Ali Afshar Avatar answered Sep 22 '22 03:09

Ali Afshar


The following selection works for me in the core expression language (returning a RowProxy object):

foo_col = sqlalchemy.sql.column('foo') s = sqlalchemy.sql.select(['*']).where(foo_col == 1) 
like image 26
Ryne Everett Avatar answered Sep 21 '22 03:09

Ryne Everett