简体   繁体   English

Django正在为Postgres生成无效的SQL

[英]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. 我是Django的新手用户,并且在使用Model.objects.all方法时遇到了一些麻烦。 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 由于某些原因,调用UsersModel.objects.all()会引发错误

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). 从Googling,我发现SQL中的此特定错误仅在Postgresql(我正在使用)上出现。 Anyone know how to get around/fix this? 有人知道如何解决/解决这个问题吗?

Sounds like the column name count is misinterpreted as aggregate function . 听起来好像列名count被误解为聚合函数

Best solution: Never use reserved words as identifiers. 最佳解决方案:切勿使用保留字作为标识符。

I ran into simillar issue when upgrading from django 1.6 to 1.9.8 on production with postgresql. 使用PostgreSQL在生产环境中从Django 1.6升级到1.9.8时遇到了类似问题。

In my case the issue was due to change in Django 1.9 requiring at least postgresql 9.1 as described here . 在我的情况的问题,是由于在Django 1.9改变至少需要9.1 PostgreSQL的描述在这里

Nice postgresql update instruction for redhat/centos here . 对于红帽/ CentOS的尼斯PostgreSQL的更新指令这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM