Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i remove a .limit() that is already applied to a query object

Tags:

sqlalchemy

question: How do i remove a .limit() that is already applied to a query object.

I have a Query which already has .limit(N) applied. Then i would like to remove the limit from the query, to have a .order_by applied. Order by has to be applied before any limit or offset.

example which fails:

query = session.query(Object).limit(10)
query = query.order_by(Object.field)

I tried doing:

query = session.query(Object).limit(10)
query = query.limit(None) # or False
query = query.order_by(Object.field)

But that does not work.

The reason i want this, is that the limit actually happens at another place as a sensible default limit.

Thanks

like image 884
jackx Avatar asked Oct 11 '12 09:10

jackx


People also ask

What does all () do in SQLAlchemy?

all() method. The Query object, when asked to return full entities, will deduplicate entries based on primary key, meaning if the same primary key value would appear in the results more than once, only one object of that primary key would be present.

What does query all () return?

all() will return all records which match our query as a list of objects.

What does SQLAlchemy query return?

It returns exactly one result or raise an exception. It applies one or more ORDER BY criterion to the query and returns the newly resulting Query. It performs a bulk update query and updates rows matched by this query in the database.

What is filter in SQLAlchemy?

The SqlAlchemy filter is one of the types and its feature for to used differently write it on the operator and conditions like where for sql but referring it to the Object Relational Mapper(ORM) tool that can be translated to Python classes on the tables and relational databases will automatically convert to the ...


1 Answers

limit(None) will cancel the limit. If that's not working, you might be on a super-old version of SQLAlchemy perhaps, I tested it all the way back to 0.6.8:

from sqlalchemy import Column, Integer
from sqlalchemy.orm import Session
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class A(Base):
    __tablename__ = "a"

    id = Column(Integer, primary_key=True)

s = Session()

q = s.query(A).limit(5)

q = q.limit(None).order_by(A.id)

print q
like image 131
zzzeek Avatar answered Oct 22 '22 07:10

zzzeek