sqlalchemy hybrid_attribute expression
问题 Assuming the following models: class Worker(Model): __tablename__ = 'workers' ... jobs = relationship('Job', back_populates='worker', order_by='desc(Job.started)', lazy='dynamic') @hybrid_property def latest_job(self): return self.jobs.first() # jobs already ordered descending @latest_job.expression def latest_job(cls): Job = db.Model._decl_class_registry.get('Job') return select([func.max(Job.started)]).where(cls.id == Job.worker_id).as_scalar() class Job(Model): ... started = db.Column(db