4

I'm a noob django user, and I'm having some trouble with the Model.objects.all method. I've got a user model: (I know keeping passwords in plaintext is bad practice, but this is just supposed to be a toy example)

class UsersModel(models.Model):
    password = models.CharField(max_length=MAX_PASSWORD_LENGTH)
    user = models.CharField(max_length=MAX_USERNAME_LENGTH, primary_key=True)
    count = models.IntegerField()

And I've got a test method that's supposed to drop all the entries in the user table:

def function(self):
    UsersModel.objects.all().delete()

For some reason, calling UsersModel.objects.all() raises the error

DatabaseError: column "cs169proj1_usersmodel.user" must appear in the GROUP BY clause or 
be used in an aggregate function
LINE 1: SELECT "cs169proj1_usersmodel"."user", "cs169proj1_usersmode...

From Googling, I've found that this particular error in SQL only comes up on Postgresql (which I'm using). Anyone know how to get around/fix this?

luc
  • 41,928
  • 25
  • 127
  • 172
sfendell
  • 5,415
  • 8
  • 25
  • 26
  • Do you have a custom manager on this model? – Dmitry Shevchenko Feb 11 '13 at 06:28
  • I don't think it is the cause of the error but you should be careful when using something else than the id as primary_key. It may cause troubles with the admin site : see http://stackoverflow.com/q/2011629/117092 – luc Feb 11 '13 at 06:41
  • 1
    Fixed it! I deleted and recreated the database, then ran manage.py syncdb. Don't know what it was, but it's gone now. – sfendell Feb 11 '13 at 07:31

2 Answers2

5

Sounds like the column name count is misinterpreted as aggregate function.

Best solution: Never use reserved words as identifiers.

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
  • This persisted after deleting the count column. I chose count as the name because this is for a class project, and I was just following the spec. – sfendell Feb 11 '13 at 07:31
  • @Yuji'Tomita'Tomita: Of course it wouldn't reference the field `count` if that was mistakenly taken to be an aggregate function. – Erwin Brandstetter Feb 12 '13 at 10:28
  • Good point... Can postgresql get confused even if django quotes all SELECT args? "table"."count". – Yuji 'Tomita' Tomita Feb 12 '13 at 17:00
  • @Yuji'Tomita'Tomita: No, PostgreSQL couldn't be confused. The column name would be `count` while the aggregate function would be `count(something)`. The confusion would have to happen earlier in the food chain. And I am not convinced that's what actually happened, but my advice is good in any case. – Erwin Brandstetter Feb 12 '13 at 21:05
  • Thanks for your responses! They have been insightful to me! – Yuji 'Tomita' Tomita Feb 12 '13 at 23:17
2

I ran into simillar issue when upgrading from django 1.6 to 1.9.8 on production with postgresql.

In my case the issue was due to change in Django 1.9 requiring at least postgresql 9.1 as described here.

Nice postgresql update instruction for redhat/centos here.

Community
  • 1
  • 1