[Sqlalchemy-tickets] Issue #3255: how to get the position of the current record of the result of a
Brought to you by:
zzzeek
|
From: Gildson B. <iss...@bi...> - 2014-11-22 23:48:28
|
New issue 3255: how to get the position of the current record of the result of a query? https://bitbucket.org/zzzeek/sqlalchemy/issue/3255/how-to-get-the-position-of-the-current Gildson Bezerra: I have 1 record table of people and I need to make a query and then immediately send the list of people to a ranking. I would like to know how best to do this operation. (Without using func.row_number()) thanks ``` #!python from sqlalchemy import create_engine, String, Integer, Column from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class People(Base): __tablename__ = 'peoples' _id = Column(Integer, primary_key=True, autoincrement=True) name = Column(String(50)) points = Column(Integer) engine = create_engine('sqlite:///db.sqlite') Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) def run(): session = Session() session.add_all([People(name='Alice', points=190), People(name='George', points=215), People(name='Nick', points=167), People(name='Alex', points=210), People(name='Paul', points=233)]) session.commit() peoples = session.query(People) \ .filter(People.points > 200) \ .order_by(People.points.desc()) \ .all() for p in peoples: # How I Can Get Position of Current Register in Peoples Here? position = 0 #position = p.? print('%s - %s (%s)' % (position, p.name, p.points)) if __name__ == '__main__': run() ``` Responsible: zzzeek |