django 1.11.x - > 2.x迁移。我得到了错误的'分组'字段

时间:2018-04-25 02:59:38

标签: python django

我刚刚升级了Django版本。我的应用程序中未修改模型或代码。

但是......我得到了不同的结果QuerySet。

打印'查询'时,'分组依据'中指定的字段不同。

型号:

class Content(models.Model):
    id_field = models.AutoField(db_column='id_', primary_key=True)
    ...
    collections = models.ManyToManyField('Collection',
                        through='CollectionMap', 
                        through_fields=('contentid', 'collectionid'))

class CollectionMap(models.Model):
    field_index = models.AutoField(db_column='_index', primary_key=True)
    collectionid = models.ForeignKey(Collection, on_delete=models.CASCADE, db_column='collectionid')
    contentid = models.ForeignKey(Content, on_delete=models.CASCADE, db_column='contentid')
    field_time = models.DateTimeField(db_column='_time')

class Collection(models.Model):
    field_index = models.AutoField(db_column='_index', primary_key=True)
    name = models.CharField(unique=True, max_length=50)
    collectionorder = models.IntegerField(db_column='collectionOrder', blank=True, null=True)
    active = models.IntegerField(blank=True, null=True)
    field_time = models.DateTimeField(db_column='_time')

代码:

Content.objects\
        .annotate(count=Count('id_field')\
        .values('id_field', 'collections__name')

查询:

1.11.X

SELECT
    Content.id_, Collection. name 
FROM Content
    LEFT OUTER JOIN Collection_Map ON (Content.id_ = Collection_Map.contentid)
    LEFT OUTER JOIN Collection ON (Collection_Map.collectionid = Collection._index)
GROUP BY Content.id_
ORDER BY Content.itemOrder DESC
LIMIT 50;

2.X

SELECT
    Content.id_, Collection. name 
FROM Content
    LEFT OUTER JOIN Collection_Map ON (Content.id_ = Collection_Map.contentid)
    LEFT OUTER JOIN Collection ON (Collection_Map.collectionid = Collection._index)
GROUP BY Content.id_, Collection. name 
ORDER BY Content.itemOrder DESC
LIMIT 50;

按字段分组:

  

版本1.11.X:GROUP BY Contentid   版本2.X:GROUP BY ContentidCollectionName

我想要这个......'...... GROUP BY Content.id ......'

我该怎么办?

0 个答案:

没有答案