Python 使用HybridProperty计算以前的同级

Python 使用HybridProperty计算以前的同级,python,mysql,sqlalchemy,Python,Mysql,Sqlalchemy,我有一个模型FollowUp,其中包含与用户和项目相关的日期和说明: class FollowUp(Base): __tablename__ = 'followup' id = Column(UUID(), primary_key=True) project_id = Column(UUID(), ForeignKey("project.id")) user_id = Column(UUID(), ForeignKey("user.id")) fol

我有一个模型
FollowUp
,其中包含与用户和项目相关的日期和说明:

 class FollowUp(Base):
    __tablename__ = 'followup'
    id =  Column(UUID(), primary_key=True)
    project_id = Column(UUID(), ForeignKey("project.id"))
    user_id = Column(UUID(), ForeignKey("user.id"))

    followup_date = Column(DateTime())
    description= Column(Unicode())
我想使用一个混合属性来返回一个子查询,该子查询统计在当前跟踪之前发生的、与从事项目的特定用户相关联的先前跟踪的数量

@hybrid_property
def previous_count(self):
    return session.query(func.count(FollowUps) \
        .filter(self.project_id == FollowUp.project_id ) \
        .filter(self.user_id == FollowUp.user_id) \
        .filter(self.followup_date > FollowUp.followup_date) \
        .as_scalar()
我将在类似于以下内容的查询中使用此选项:

session.query(Project.title, User.name, FollowUp.previous_count) \
    .filter(Project.id == @SOME_PROJECT_ID)
问题在于,
hybrid_属性
不使用主查询,主查询返回:

选择
计数(:参数2)作为计数1
从…起
后续行动
哪里
和followup.project\u id=followup.project\u id
和followup.user\u id=followup.user\u id
和followup.followup\u日期
我应该做什么改变才能让它工作?我试图创建一个别名:

@hybrid_property
def previous_count(self):
    alias_followup = aliased(FollowUp)
    return session.query(func.count(alias_followup)) \
        .join(alias_followup) \
        .filter(self.project_id == alias_followup.project_id ) \
        .filter(self.user_id == alias_followup.user_id) \
        .filter(self.followup_date > alias_followup.followup_date) \
        .as_scalar()
它给出了以下错误:

NoInspectionAvailable: No inspection system is available for object of type <type 'NoneType'>
但它给了我另一个错误:

InvalidRequestError: Can't construct a join from <AliasedClass at 0x7f04ec7b6c90; FollowUp> to <AliasedClass at 0x7f04ec7b6c90; FollowUp>, they are the same entity
InvalidRequestError:无法构造从到的联接,它们是同一实体

您使用别名是正确的,但没有正确使用别名。另外,根据我对你的回答,你需要两个部分的混合财产

@hybrid_property
def previous_count(self):
    # the python property, queries the database and returns a count
    cls = self.__class__
    return session.query(func.count(cls.id)).filter(cls.project == self.project, cls.ts < self.ts).scalar()

@previous_count.expression
def previous_count(cls):
    # the sql property, constructs a scalar subquery with an alias
    other = aliased(cls)
    return session.query(func.count(other.id)).filter(other.project_id == cls.project_id, other.ts < cls.ts).as_scalar()
InvalidRequestError: Can't construct a join from <AliasedClass at 0x7f04ec7b6c90; FollowUp> to <AliasedClass at 0x7f04ec7b6c90; FollowUp>, they are the same entity
@hybrid_property
def previous_count(self):
    # the python property, queries the database and returns a count
    cls = self.__class__
    return session.query(func.count(cls.id)).filter(cls.project == self.project, cls.ts < self.ts).scalar()

@previous_count.expression
def previous_count(cls):
    # the sql property, constructs a scalar subquery with an alias
    other = aliased(cls)
    return session.query(func.count(other.id)).filter(other.project_id == cls.project_id, other.ts < cls.ts).as_scalar()
from datetime import datetime
from sqlalchemy import create_engine, Column, Integer, DateTime, ForeignKey, func
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import Session, relationship, aliased

engine = create_engine('sqlite:///', echo=True)
session = Session(bind=engine)
Base = declarative_base(bind=engine)


class Project(Base):
    __tablename__ = 'project'

    id = Column(Integer, primary_key=True)


class Followup(Base):
    __tablename__ = 'followup'

    id = Column(Integer, primary_key=True)
    project_id = Column(Integer, ForeignKey(Project.id))
    ts = Column(DateTime, nullable=False)

    project = relationship(Project, backref='followups')

    @hybrid_property
    def previous_count(self):
        # the python property, queries the database and returns a count
        cls = self.__class__
        return session.query(func.count(cls.id)).filter(cls.project == self.project, cls.ts < self.ts).scalar()

    @previous_count.expression
    def previous_count(cls):
        # the sql property, constructs a scalar subquery with an alias
        other = aliased(cls)
        return session.query(func.count(other.id)).filter(other.project_id == cls.project_id, other.ts < cls.ts).as_scalar()


Base.metadata.create_all()

p1 = Project()
p2 = Project()

f1 = Followup(project=p1, ts=datetime(2014, 1, 1))
f2 = Followup(project=p1, ts=datetime(2014, 2, 1))
f3 = Followup(project=p1, ts=datetime(2014, 3, 1))
f4 = Followup(project=p1, ts=datetime(2014, 4, 1))
f5 = Followup(project=p2, ts=datetime(2014, 5, 1))

session.add_all((p1, p2))
session.commit()

print(session.query(Followup.id, Followup.previous_count).all())
[(1, 0), (2, 1), (3, 2), (4, 3), (5, 0)]