11

If I've got an SQLAlchemy ORM query:

admin_users = Session.query(User).filter_by(is_admin=True)

Is it possible to modify the columns returned by that query?

For example, so that I could select only the User.id column, and use that in a sub query:

admin_email_addresses = Session.query(EmailAddress)\
 .filter(EmailAddress.user_id.in_(admin_users.select_columns(User.id))

Note: the .values() method will not work, as it executes the query and returns an iterable of results (so, ex, EmailAddress.user_id.in_(admin_users.values(User.id)) will perform two queries, not one).

I know that I could modify the first query to be Session.query(User.id), but I'm specifically wondering how I could modify the columns returned by a query.

asked May 20, 2012 at 21:49

2 Answers 2

21

I feel your pain on the values() thing. In 0.6.5 I added with_entities() which is just like values() except doesn't iterate:

q = q.with_entities(User.id)
user
5,4417 gold badges53 silver badges66 bronze badges
answered May 21, 2012 at 13:44

1 Comment

For anyone else looking at this post SQlAlchemy 2.0 - Select.with_entities() has been replaced by Select.with_only_columns(): docs.sqlalchemy.org/en/20/core/…
2

Assuming that your Address.user_id defines a ForeignKey, the query below will do the job more efficiently compared to IN operator:

admin_email_addresses = session.query(EmailAddress).\
 join(User).filter(User.is_admin==True)

If you do not have a ForeignKey (although you should), you can specify the join condition explicitely:

admin_email_addresses = session.query(EmailAddress).\
 join(User, User.id==EmailAddress.user_id).filter(User.is_admin==True)

But if you really would like to do it with in_ operator, here you go (note the subquery):

subq = session.query(User.id).filter(User.is_admin==True).subquery()
admin_email_addresses = session.query(EmailAddress).\
 filter(EmailAddress.user_id.in_(subq)) 
answered May 21, 2012 at 6:57

1 Comment

It's true that, in the example I provided, a JOIN is the correct solution. However, since it doesn't address the question I actually asked, I'm going to be accepting zzzeek's answer.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.