我有一个模型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属性返回一个子查询,该子查询计算在与项目的特定用户关联的当前后续操作之前发生的先前后续操作的数量。
@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_property
不使用主查询,后者返回:
SELECT
count(:param_2) AS count_1
FROM
followup
WHERE
AND followup.project_id = followup.project_id
AND followup.user_id = followup.user_id
AND followup.followup_date < followup.followup_date
我应该更改什么才能使其正常工作?我试图创建一个别名:
@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: Could not find a FROM clause to join from. Tried joining to <AliasedClass at 0x7fde5c0d9e50; Suivi>, but got: Can't find any foreign key relationships between 'followup' and 'previous_count'.
为了测试目的,我删除了func.count
和另一列,我将join子句修改为:
.join(alias_followup , self.id == FollowUp.id)
但它给了我另一个错误:
InvalidRequestError: Can't construct a join from <AliasedClass at 0x7f04ec7b6c90; FollowUp> to <AliasedClass at 0x7f04ec7b6c90; FollowUp>, they are the same entity
答案 0 :(得分:2)
使用别名是正确的,但没有正确使用它。另外,根据我对other question的回答,您需要两个部分来处理混合属性。
@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())
创建了两个项目,一个有4个后续,另一个有1个。然后查询后续内容以打印其ID和先前的计数。生成以下查询:
SELECT followup.id AS followup_id, (SELECT count(followup_1.id) AS count_1
FROM followup AS followup_1
WHERE followup_1.project_id = followup.project_id AND followup_1.ts < followup.ts) AS anon_1
FROM followup
输出结果为:
[(1, 0), (2, 1), (3, 2), (4, 3), (5, 0)]