sfendell
sfendell

Reputation: 5595

Django is generating invalid SQL for Postgres

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?

Upvotes: 4

Views: 903

Answers (2)

pawel lukaszewicz
pawel lukaszewicz

Reputation: 575

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.

Upvotes: 2

Erwin Brandstetter
Erwin Brandstetter

Reputation: 656301

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

Best solution: Never use reserved words as identifiers.

Upvotes: 5

Related Questions