为什么我的数据库会抛出错误?

时间:2013-10-25 03:00:27

标签: python mysql django

我正在尝试通过管理网站访问我的应用程序,同时执行Django教程并收到此错误:

DatabaseError at /admin/polls/poll/ (1054, "Unknown column 'polls_poll.question' in 'field list'")

我最初设置我的模型使用Question类,同时错误地使用1.6版本的教程,但切换到1.54版本并且不得不将我的Question类重命名为Polls。然后我重新启用了数据库。我正在使用MySQL。

现在,每次我点击管理网站上的“民意调查”时,都会抛出上述错误。

我已经尝试刷新我的数据库并重新同步但这不起作用。

这是我的 models.py 代码:

from django.db import models
import datetime
from django.utils import timezone

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __unicode__(self): 
        return self.question

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

class Choice(models.Model):

    poll = models.ForeignKey(Poll)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

    def __unicode__(self):
        return self.choice_text

这是完整的追溯:

Internal Server Error: /admin/polls/poll/
Traceback (most recent call last):
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/core/handlers/base.py", line 115, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/contrib/admin/options.py", line 372, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/utils/decorators.py", line 91, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/views/decorators/cache.py", line 89, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 202, in inner
    return view(request, *args, **kwargs)
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/utils/decorators.py", line 25, in _wrapper
    return bound_func(*args, **kwargs)
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/utils/decorators.py", line 91, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/utils/decorators.py", line 21, in bound_func
    return func(self, *args2, **kwargs2)
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/contrib/admin/options.py", line 1285, in changelist_view
    'selection_note': _('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)},
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/db/models/query.py", line 106, in __len__
    self._result_cache = list(self.iterator())
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/db/models/query.py", line 317, in iterator
    for row in compiler.results_iter():
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 775, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 840, in execute_sql
    cursor.execute(sql, params)
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/db/backends/util.py", line 41, in execute
    return self.cursor.execute(sql, params)
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 128, in execute
    six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e.args)), sys.exc_info()[2])
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 120, in execute
    return self.cursor.execute(query, args)
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/MySQLdb/cursors.py", line 201, in execute
    self.errorhandler(self, exc, value)
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
DatabaseError: (1054, "Unknown column 'polls_poll.question' in 'field list'")

1 个答案:

答案 0 :(得分:2)

Django的syncdb命令只会创建尚不存在的表。因此,如果您之前已使用不同版本的模型同步数据库,则字段名称中的任何更改都不会出现在数据库表中。

要验证,请使用mysql客户端检查您的polls_poll表。

mysql> DESCRIBE polls_poll;

您可能会看到question字段丢失。

根据评论中的建议,您可以擦除整个数据库,但侵入性较小的解决方案只是删除polls_poll表并再次运行syncdb

mysql> DROP TABLE polls_poll;
$ manage.py syncdb

这当然会删除与民意调查相关的所有数据。