【发布时间】:2012-11-24 00:51:49
【问题描述】:
我在 Heroku 上为我的 Django 应用程序使用 postgresql。当我尝试为我的帖子发表评论时,我有时会收到此错误(同样,有时并非总是如此)。
尽管出现错误,注释仍然保存,但是save()后面的所有代码都没有执行。
这个问题只发生在 postgresql 上。在我使用 sqlite 的本地主机上,一切正常。
我不确定这是什么原因。
这就是我的模型的样子
class Comment(models.Model):
post = models.ForeignKey(post)
date = models.DateTimeField(auto_now_add=True)
comment = models.TextField()
comment_user = models.ForeignKey(User)
这就是我的评论模型的样子。 那么是不是因为我没有添加 max_length 进行评论? 这是回溯
DatabaseError at /post/114/
value too long for type character varying(10)
Request Method: POST
Request URL: http://www.mysite.com/post/114/
Django Version: 1.4.1
Exception Type: DatabaseError
Exception Value:
value too long for type character varying(10)
Exception Location: /app/.heroku/venv/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py in execute, line 52
Python Executable: /app/.heroku/venv/bin/python2.7
Python Version: 2.7.2
Python Path:
['/app',
'/app/.heroku/venv/bin',
'/app/.heroku/venv/lib/python2.7/site-packages/pip-1.1-py2.7.egg',
'/app/.heroku/venv/lib/python2.7/site-packages/distribute-0.6.31-py2.7.egg',
'/app',
'/app/.heroku/venv/lib/python27.zip',
'/app/.heroku/venv/lib/python2.7',
'/app/.heroku/venv/lib/python2.7/plat-linux2',
'/app/.heroku/venv/lib/python2.7/lib-tk',
'/app/.heroku/venv/lib/python2.7/lib-old',
'/app/.heroku/venv/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7',
'/usr/local/lib/python2.7/plat-linux2',
'/usr/local/lib/python2.7/lib-tk',
'/app/.heroku/venv/lib/python2.7/site-packages',
'/app/.heroku/venv/lib/python2.7/site-packages/PIL']
Server time: Wed, 5 Dec 2012 20:41:39 -0600
【问题讨论】:
-
启用日志记录以查看究竟是哪个查询导致了哪个表上的错误,然后从那里开始工作。
-
我也添加了。不过好像也没多大用处
-
AFAIK 将字符大小设置为小于 255 是没有意义的;它应该消耗相同数量的磁盘空间。
-
@Mark:实际上,对于 PostgreSQL 中的任何
n,varchar(n)的意义不大,除非您需要限制,否则只需使用text; PostgreSQL 内部对待text、varchar和varchar(n)相同(除了varchar(n)的长度检查)。 -
@Mark:MySQL 有一大堆字符串类型,长度的字节数不同,我不能说其他的。 PostgreSQL 使用 1 或 4 个字节作为长度:"The storage requirement for a short string (up to 126 bytes) is 1 byte plus the actual string, which includes the space padding in the case of
character. Longer strings have 4 bytes of overhead instead of 1. Long strings are compressed by the system automatically..."。一两个字节不再值得麻烦了:)
标签: django postgresql heroku