'Sqlalchemy does not work with pagination

I am totally new with flask and sqlalchemy. I try to make one web site just to get better understanding of flask. There are plenty of tutorials and they are mostly use sqlite. For my purpose I use postgres. and I need one page where I can use pagination. But all the time I am getting the error

AttributeError: 'Query' object has no attribute 'paginate'

my database

uri = os.environ.get('DATABASE_URL', 'postgres://login:[email protected]/db_name')
engine = create_engine(uri, convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,
                     autoflush=False,
                     bind=engine))

Base = declarative_base()
Base.query = db_session.query_property()    

simple model

class User(Base):
    __tablename__ = 'user'

    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(Unicode(100), nullable=False)
    ...

then I use paginate

users = User.query.paginate(1, 5, False)

The same error with get_or_404() and first_or_404(). normal get or first works as usual.

I will appreciate any advice!



Solution 1:[1]

use limit() and offset() for pagination using mysql sqlalchemy.

query = User.query.limit().offset()
data = query.all()

you'll get the desired results.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Prakash