[英]Sqlalchemy keep object while using func.sum
I'm trying to use the func.sum( function while still keeping the object as output for serialization with Marshmallow.我正在尝试使用 func.sum( function ,同时仍将 object 保持为 output 与 Marshmallow 进行序列化。
query = (
session.query(Employee, func.sum(JobEntry.number_registered).label('hours_on_project'))
.join(JobEntry, Employee.employee_hashkey==JobEntry.employee_hashkey)
.filter(JobEntry.job_number.like('1011811%'))
.group_by(Employee)
.order_by(sa.desc('hours_on_project'))
).first()
The query returns:查询返回:
(<Employee object>, 8327.0)
I would like it to return the JobEntry object in addition to the sum:除了总和之外,我还希望它返回 JobEntry object:
(<Employee object>, <JobEntry object>, 8327.0)
Tried adding JobEntry in the query but then I get an OperationalError..尝试在查询中添加 JobEntry 但后来我得到一个 OperationalError ..
How do I add the JobEntry object as output while still using func.sum?如何在仍使用 func.sum 的同时将 JobEntry object 添加为 output?
The JobEntry table is not 1:1 with the Employee table. JobEntry 表与 Employee 表不是 1:1 的。 If they where 1:1 the answer would be adding JobEntry to the query and group by like so:
如果他们 1:1 的答案是将 JobEntry 添加到查询中并按如下方式分组:
query = (
session.query(Employee, JobEntry, func.sum(JobEntry.number_registered).label('hours_on_project'))
.join(JobEntry, Employee.employee_hashkey==JobEntry.employee_hashkey)
.filter(JobEntry.job_number.like('1011811%'))
.group_by(Employee, JobEntry)
.order_by(sa.desc('hours_on_project'))
) )
Adding JobEntry to 1:M is possible, but would be wrong in my case..将 JobEntry 添加到 1:M 是可能的,但在我的情况下是错误的..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.