Skip to content

Instantly share code, notes, and snippets.

@darkpixel
Last active February 29, 2016 19:59
Show Gist options
  • Save darkpixel/e8f494038d5192225901 to your computer and use it in GitHub Desktop.
Save darkpixel/e8f494038d5192225901 to your computer and use it in GitHub Desktop.
ProgrammingError: column "company.owner_id" must appear in the GROUP BY clause or be used in the aggregate function
Please ignore the horrible DB schema. It's not mine. It's an introspection of a horrible Windows ticket app.
I upgraded from Django 1.8.x to 1.9 and the following query that I use when gathering stats (Top ticket creators) broke:
>>> Company.objects.all().annotate(ticketcount=Count('srservice'))
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/aaron/.virtualenvs/intranet/lib/python2.7/site-packages/django/db/models/query.py", line 234, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/home/aaron/.virtualenvs/intranet/lib/python2.7/site-packages/django/db/models/query.py", line 258, in __iter__
self._fetch_all()
File "/home/aaron/.virtualenvs/intranet/lib/python2.7/site-packages/django/db/models/query.py", line 1074, in _fetch_all
self._result_cache = list(self.iterator())
File "/home/aaron/.virtualenvs/intranet/lib/python2.7/site-packages/django/db/models/query.py", line 52, in __iter__
results = compiler.execute_sql()
File "/home/aaron/.virtualenvs/intranet/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 848, in execute_sql
cursor.execute(sql, params)
File "/home/aaron/.virtualenvs/intranet/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/aaron/.virtualenvs/intranet/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/aaron/.virtualenvs/intranet/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/aaron/.virtualenvs/intranet/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
ProgrammingError: column "company.owner_id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT "company"."owner_id", "company"."company_recid", "com...
^
>>>
# models.py
class SrService(models.Model):
owner_id = models.IntegerField(blank=True, null=True)
sr_service_recid = models.IntegerField(primary_key=True)
contract_recid = models.IntegerField(blank=True, null=True)
company_recid = models.ForeignKey('Company', db_column='company_recid')
contact_recid = models.ForeignKey('Contact', db_column='contact_recid', blank=True, null=True)
summary = models.CharField(max_length=100, blank=True)
...snip...
def __unicode__(self):
return u"%s - %s" % (self.sr_service_recid, self.summary)
class Meta:
managed = False
db_table = 'sr_service'
get_latest_by = 'sr_service_recid'
ordering = ['pk']
class Company(models.Model):
owner_id = models.IntegerField(blank=True, null=True)
company_recid = models.IntegerField(primary_key=True)
company_id = models.CharField(max_length=50, blank=True)
company_name = models.CharField(max_length=50, blank=True)
...snip...
def __unicode__(self):
return u'%(company_name)s' % self.__dict__
class Meta:
managed = False
ordering = ['company_name',]
db_table = 'company'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment