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?

3
  • Do you have a custom manager on this model? Commented Feb 11, 2013 at 6: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 stackoverflow.com/q/2011629/117092 Commented Feb 11, 2013 at 6: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. Commented Feb 11, 2013 at 7:31

2 Answers 2

5

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

Best solution: Never use reserved words as identifiers.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
@Yuji'Tomita'Tomita: Of course it wouldn't reference the field count if that was mistakenly taken to be an aggregate function.
Good point... Can postgresql get confused even if django quotes all SELECT args? "table"."count".
@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.
Thanks for your responses! They have been insightful to me!
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.