Django - 选择对象时错误:“ProgrammingError: 运算符不存在:字符变体 = 整数”
我的 models.py 文件里包含了
class Patient(models.Model):
cpf_id = models.CharField(max_length=15, unique=True)
name_txt = models.CharField(max_length=50)
nr_record = models.AutoField(primary_key=True)
数据库是 PostgreSQL。
当我尝试用
>>> Patient.objects.get(nr_record=1)
去选择一个对象时,我遇到了这个错误
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 151, in get
return self.get_queryset().get(*args, **kwargs)
.
.
.
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
ProgrammingError: operator does not exist: character varying = integer
LINE 1: ...d" FROM "quiz_patient" WHERE "quiz_patient"."nr_record" = 1
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
我还是 Django-python 的新手。不过我知道 models.Autofield 是整数类型。
我在网上搜索过,虽然看到有相同错误信息的帖子,但没有找到和我问题一样的情况。
1 个回答
2
这个问题很可能出在你的数据库上。也许你之前的模型版本中,nr_record 这个字段不是整数类型?
你可以试着把数据库中的 nr_record 列删掉,然后再重新添加一次。