column "" must appear in the GROUP BY clause or be used in an aggregate function

319 views
Skip to first unread message

muin

unread,
Oct 14, 2018, 9:38:44 PM10/14/18
to Django users

Django 2.1.2, Postgres 10

Model:
class Product(models.Model):
product_id = models.AutoField("产品ID", primary_key=True)
...

class SpecItem(models.Model):
spec_id = models.AutoField(primary_key=True)
product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name="product_specs", null=True)
price = models.FloatField("价格", default=0)
...

When exwcute
qs = Product.objects.filter(valid=1)
serializer = ProductSerializer(
qs.annotate(cnt=Count("product_specs"), min_price=Min("product_specs__price")).filter(cnt__gt=0)
.order_by("min_price")[offset:offset + limit],
context={"request": request},
many=True)

It says django.db.utils.ProgrammingError: column "prome_product.product_name" must appear in the GROUP BY clause or be used in an aggregate function


Can anyone help me?

ludovic coues

unread,
Oct 15, 2018, 3:19:11 AM10/15/18
to django...@googlegroups.com
I cannot find in the code you shared "prome_product" so I assume there is missing code or a typo.

The ProgrammingError should come with a line number or a stack trace where the line causing issue is roughly in the middle. If it's the serializer line, I suggest you split it, doing the annotate before calling serializer.

Query set are "lazy loading", they don't fetch from the database untill they have to. While it's great most of the time, that cause issue while debugging. Try to call list(qs.annotate(...)[offset:offset+limit]) to force the query set to execute.

This won't give you an answer but that might help you to find the issue.

Good luck!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/64c1b2f0-6125-45e0-a08b-7d5d3426f16d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted

muin

unread,
Oct 15, 2018, 5:08:45 AM10/15/18
to Django users
Thank you for your reply,"prome" is my app name.

It runs well on django1.11 and python2.7.6. This error happens when upgrade to django2.1.2 and python3.6.6

I had changed to MySQL,it runs well from the same code. And sqlite is also OK.
Reply all
Reply to author
Forward
0 new messages