SQLAlchemy: get the object with the most recent date
Question:
How do I query a table for the object with the most recent table?
I’ve got a table which holds
class Ticker(Base):
updated = Column('updated', DATETIME, index=False, nullable=False,primary_key=True)
high = Column('high', FLOAT, index=False, nullable=False)
I’m trying to find out how I can get the object with the most recent updated field? Currently I’m doing the following:
maxdate = db_session.query(func.max(Ticker.updated)).first()[0]
Ticker.query.filter(Ticker.updated == maxdate).first()
I was wondering if there is a more efficient/shorter way to do this?
Answers:
Same as this with a small variation in syntax:
session.query(Ticker).order_by(Ticker.updated.desc()).limit(1)
How do I query a table for the object with the most recent table?
I’ve got a table which holds
class Ticker(Base):
updated = Column('updated', DATETIME, index=False, nullable=False,primary_key=True)
high = Column('high', FLOAT, index=False, nullable=False)
I’m trying to find out how I can get the object with the most recent updated field? Currently I’m doing the following:
maxdate = db_session.query(func.max(Ticker.updated)).first()[0]
Ticker.query.filter(Ticker.updated == maxdate).first()
I was wondering if there is a more efficient/shorter way to do this?
Same as this with a small variation in syntax:
session.query(Ticker).order_by(Ticker.updated.desc()).limit(1)