gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 08:20:27 39 4
gpt4 key购买 nike

我在学习 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'")

最佳答案

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

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

mysql> DESCRIBE polls_poll;

您可能会发现缺少问题字段。

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

mysql> DROP TABLE polls_poll;
$ manage.py syncdb

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

关于python - 为什么我的数据库抛出错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19580364/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com