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
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.
all() will return all records which match our query as a list of objects.
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.
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 ...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With