Iāve got a similar (but not identical) table in my test environment named Author. This is what Iām getting:
In [1]: a=Author.objects.values('name').annotate(cnt=Count('name')).order_by('?')
In [2]: print(a.query)
SELECT "fs_author"."name", COUNT("fs_author"."name") AS "cnt" FROM "fs_author" GROUP BY "fs_author"."name" ORDER BY RANDOM() ASC
This leads me to believe thereās some information associated with this that youāre not showing here.
What are you looking at that is indicating that Django is creating that sql?
How are you running that query? Is that something from your shell? In a view?
Is there a custom manager involved?
Is there anything else that might be affecting the query being generated?
running python manage.py runserver command.
and called from views.py
from .models import Account
from django.db.models import Count
def index(request):
a = Account.objects.values('plan').annotate(count=Count('plan')).order_by('?')
print(a.query)
SELECT "accounts"."plan", COUNT("accounts"."plan") AS "count" FROM "accounts" GROUP BY "accounts"."plan", RANDOM() ORDER BY RANDOM() ASC
Iām not using a custom manager.
Itās happening in a simple project I just created, so I canāt find any other potential impact.
I verified Iām getting the same results as you with Django 3.1, so this appears to be something that was fixed between 3.1 and 3.2.
I took a quick look through the release notes for 3.2, but I donāt see anything quickly that would explain the change in behavior. If youāre really interested, you could try searching through the issues to see if you can find when / where the patch was applied.