Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python sqlAlchemy中的和域_Python_Sqlalchemy - Fatal编程技术网

Python sqlAlchemy中的和域

Python sqlAlchemy中的和域,python,sqlalchemy,Python,Sqlalchemy,我最近升级到了sqlalchemy的最新版本,我的一些代码不再工作。我很难找到修复方法,需要一只手 在此之前,查询是这样出现的 self.db.query(Drive).filter(Drive.package_id==package.package_id)\ .filter(Drive.wipe_end!=None).sum(Drive.wipe_end - Drive.wipe_start) 这在检索某些持续时间的总和之前是有效的,但现在我得到以下错误: 'Query' objec

我最近升级到了sqlalchemy的最新版本,我的一些代码不再工作。我很难找到修复方法,需要一只手

在此之前,查询是这样出现的

self.db.query(Drive).filter(Drive.package_id==package.package_id)\
    .filter(Drive.wipe_end!=None).sum(Drive.wipe_end - Drive.wipe_start)
这在检索某些持续时间的总和之前是有效的,但现在我得到以下错误:

'Query' object has no attribute 'sum'
我所做的任何谷歌搜索都会得到几年前的信息。

我相信您需要“func”包中的sum()函数:

在SQLAlchemy 1.1.13(2017年8月3日发布)中,使用
sum()
的语法如下:

from sqlalchemy import func
from apps.mystuff.models import MyModel

some_value = "hello"

result = MyModel.query.with_entities(
             func.sum(MyModel.MyColumn).label("mySum")
         ).filter_by(
             MyValue=some_value
         ).first()

# Depending on the column datatype, it's either int:
print(result.mySum)

# Or if the data is decimal/float:
print(float(result.mySum))
与原始答案相比,主要区别在于:
query(func.sum())

从版本0.6.5开始,语法已更改为此:
query.with_实体(func.sum())


为什么使用标量?如何舍入这个数字呢?我得到了
TypeError:“BaseQuery”对象在Python3中是不可调用的
,在SQLAlchemy 1.12中使用:
vots\u sum=VotesReleases.query(func.sum(VotesReleases.Vote)).filter\u by(ReleaseID=release\u id.all()
。或者
func.sum()
不代表SQL
sum()
?我发现了现代SQLAlchemy的语法。我发布了一个答案。是的,这个很好,没有任何错误!!
from sqlalchemy import func
from apps.mystuff.models import MyModel

some_value = "hello"

result = MyModel.query.with_entities(
             func.sum(MyModel.MyColumn).label("mySum")
         ).filter_by(
             MyValue=some_value
         ).first()

# Depending on the column datatype, it's either int:
print(result.mySum)

# Or if the data is decimal/float:
print(float(result.mySum))