1

I'm wanting to do pagination, I'm using PostgreSQL. To avoid doing the query twice, I'm using the feature described at https://stackoverflow.com/a/8242764

How can I add the full_count column to the list of columns fetched in my query. I can use query.add_columns, but that column is not accessible after calling query.all()

Is this possible in SQLAlchemy?

1 Answer 1

4

Can you provide the original code for the query you want to augment? Are you using SQLAlchemy core or declarative ORM? For the latter it will look like this:

from sqlalchemy import func

items = session.query(Model, func.count().over().label('full_count')).all()
obj = items[0].Model
count = items[0].full_count
Sign up to request clarification or add additional context in comments.

3 Comments

I'm not sure what I was doing before, but it definitely wasn't what you've suggested. Your solution was exactly what I needed.
Is there a way to do this where the 'full_count' field gets placed directly on the Model? So items[0] would return the same as items[0].Model in your example above, except that items[0].full_count would contain the result of the func.count()?
You may be able to get what you want using query-time SQL expressions. Here's an example.

Your Answer

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.