Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.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
Mysql Django Postgres聚合函数_Mysql_Django_Postgresql_Aggregate Functions - Fatal编程技术网

Mysql Django Postgres聚合函数

Mysql Django Postgres聚合函数,mysql,django,postgresql,aggregate-functions,Mysql,Django,Postgresql,Aggregate Functions,我想在模型中的列上使用聚合函数 以下是我的模型:- class ShipmentWeightMapping(models.Model): weight = models.CharField(max_length = 255) status = models.CharField(max_length = 255) time = models.DateTimeField( auto_now_add = True) shipment_id = models.Foreig

我想在模型中的列上使用聚合函数

以下是我的模型:-

class ShipmentWeightMapping(models.Model):
    weight = models.CharField(max_length = 255)
    status = models.CharField(max_length = 255)
    time = models.DateTimeField( auto_now_add = True)
    shipment_id = models.ForeignKey('Shipment', related_name = 'weights')
以下是我的聚合查询:-

shipment_weight_obj = ShipmentWeightMapping.objects.filter(shipment_id__in = shipment_id_list).aggregate(Avg('weight'),Max('weight'),Min('weight'),Sum('weight'))
total_weight = shipment_weight_obj['weight__sum']
max_weight = shipment_weight_obj['weight__max']
min_weight = shipment_weight_obj['weight__min']
avg_weight = shipment_weight_obj['weight__avg']
上面的代码使用MySQL作为DB运行,但与postgres一起使用时返回一个错误,返回一个错误:-

LINE 1: SELECT SUM("data_shipmentweightmapping"."weight") AS "weight...
[Tue Jul 08 04:05:38 2014] [error]                ^
[Tue Jul 08 04:05:38 2014] [error] HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
这是怎么回事?我知道权重是一个
char
字段,我正在添加一个
char
字段,我想这在postgres中是不允许的,但是如何改变我的查询使其工作呢?还有,为什么它在MySql中工作而在postgres中不工作

我知道权重是一个char字段,我正在添加一个char字段,我想这在postgres中是不允许的,但是如何改变我的查询使其工作呢

要么:

  • 重量
    使用合适的数据类型-可能是
    float4
    float8
    双精度
    ),但如果需要,可能是
    数值
    ;或

  • 在聚合之前强制转换为数值类型,因此可以运行
    SUM(强制转换(“data\u shipmentweightmapping”,“weight”作为float4))

还有,为什么它在MySql中工作而在postgres中不工作

MySQL可以让你做各种PostgreSQL没有做的疯狂的事情。比如将一堆文本字段添加到一起,然后隐式地将它们转换为数字。插入零作为日期。比较空值是否相等


如果您在严格的ANSI模式下运行MySQL(您应该始终这样做),那么它会让您更清楚地了解它允许您做或不允许做的奇怪事情。

在Django ORM中如何做?@user1162512理想情况下:在您的模型中将列类型更改为正确的类型。如果您的意思是“如何在Django ORM查询中键入强制转换列”。。。不知道。我建议在这个问题上发布一个Django特有的问题,并链接回这个问题以了解上下文。