Django group by无法正常工作

时间:2018-07-24 18:00:15

标签: sql django django-models group-by django-orm

这是我的课程:

class Tizz(models.Model):
    key = models.PositiveIntegerField(default=0)
    name = models.CharField(max_length=20)
    value = models.CharField(max_length=500)

我的查询:

qs = Tizz.objects.values('key').annotate(
    hand=Concat(Case(When(column_name='hand', then=F('value'))), Value(''), output_field=CharField()),
    maintain=Concat(Case(When(column_name='maintain', then=F('value'))), Value(''), output_field=CharField()),
    pkey=Count('key')
)

str(qs.query)给我错误的SQL

SELECT "app_tizz"."key", COALESCE(CASE WHEN "app_tizz"."name" = hand THEN "app_tizz"."value" ELSE NULL END, ) || COALESCE(, ) AS "hand", COALESCE(CASE WHEN "app_tizz"."name" = maintain THEN "app_tizz"."value" ELSE NULL END, ) || COALESCE(, ) AS "maintain", COUNT("app_tizz"."key") AS "pkey" FROM "app_tizz" GROUP BY "app_tizz"."primary_key", COALESCE(CASE WHEN "app_celltizz"."name" = hand THEN "app_tizz"."value" ELSE NULL END, ) || COALESCE(, ), COALESCE(CASE WHEN "app_tizz"."name" = maintain THEN "app_tizz"."value" ELSE NULL END, ) || COALESCE(, )

在GROUP BY之后始终添加其他情况。为什么会这样?

0 个答案:

没有答案