Django中的错误GROUP BY字段注释查询

时间:2015-06-25 12:54:23

标签: sql django django-models django-orm django-aggregation

由非常笨拙的骑车模型引起的原始问题参考:

# A -> B -> A

class A:
    b = models.ForeignKey('B', null=True, blank=True)

class B:
    a = models.ForeignKey('A')

现在,当我尝试注释查询时,它总是使用来自LEFT OUTER JOIN的GROUP BY a id(在下面的示例中为 T3.id )而不是 a.id

示例:

A.objects.select_related('b', 'b__a').annotate(reviews=Count('reviews'))

生成的SQL:

SELECT 
    `a`.`id`,
    `b`.`id`,
    T3.`id`,
FROM
    `a`
        LEFT OUTER JOIN
    `b` ON (`a`.`b_id` = `b`.`id`)
        LEFT OUTER JOIN
    `a` T3 ON (`b`.`a_id` = T3.`id`)
WHERE
    `a`.`id` IN (1, 2, 3, 4, 5)
GROUP BY T3.`id`
ORDER BY NULL;

我知道我可以做下一件事:

  1. 更改模型不要做骑行参考(遗憾的是现在不能这样做)
  2. 可以使用.extra()代替注释(我试着避免使用它)
  3. 删除.select_related()调用(由于性能问题而无法执行)
  4. UPD:使用GROUP BY T3.id 将排除结果,其中a.b ==无

    对我来说最好的解决方案是在GROUP BY子句中指定正确的字段,但我不知道如何。可能吗?有没有其他方法来解决这个问题?感谢。

1 个答案:

答案 0 :(得分:0)

打开Django编译器:

def collapse_group_by(self, expressions, having):
    # If the DB can group by primary key, then group by the primary key of
    # query's main model. Note that for PostgreSQL the GROUP BY clause must
    # include the primary key of every table, but for MySQL it is enough to
    # have the main table's primary key. Currently only the MySQL form is
    # implemented.
    # MySQLism: however, columns in HAVING clause must be added to the
    # GROUP BY.
    if self.connection.features.allows_group_by_pk:
        # The logic here is: if the main model's primary key is in the
        # query, then set new_expressions to that field. If that happens,
        # then also add having expressions to group by.
        pk = None
        for expr in expressions:
            if (expr.output_field.primary_key and
                    getattr(expr.output_field, 'model') == self.query.model):
                pk = expr
                # HERE BREAKPOINT REQUIRED
        if pk:
            expressions = [pk] + [expr for expr in expressions if expr in having]
    return expressions

所以,collapse_group_by函数不会停止查找pk,即使它已经找到了,这就是为什么分组由T3.id而不是a.id完成(因此我缺少结果)。 要解决此问题,for循环内部需要断点(在注释中标记)。

UPD:已在Django 1.8.2版https://code.djangoproject.com/ticket/24748

中修复此问题