Django 模型(1054,“'字段列表'中的未知列") [英] Django Models (1054, "Unknown column in 'field list'")
问题描述
不知道为什么会弹出这个错误.这是我创建的模型 -
No idea why this error is popping up. Here are the models I created -
from django.db import models
from django.contrib.auth.models import User
class Shows(models.Model):
showid= models.CharField(max_length=10, unique=True, db_index=True)
name = models.CharField(max_length=256, db_index=True)
aka = models.CharField(max_length=256, db_index=True)
score = models.FloatField()
class UserShow(models.Model):
user = models.ForeignKey(User)
show = models.ForeignKey(Shows)
这是我访问这些模型的视图 -
Here is the view from which I access these models -
from django.http import HttpResponse
from django.template import Context
from django.template.loader import get_template
from django.http import HttpResponse, Http404
from django.contrib.auth.models import User
def user_page(request, username):
try:
user = User.objects.get(username=username)
except:
raise Http404('Requested user not found.')
shows = user.usershow_set.all()
template = get_template('user_page.html')
variables = Context({
'username': username,
'shows' : shows})
output = template.render(variables)
return HttpResponse(output)
此时我收到一个错误 -
At this point I get an error -
OperationalError:(1054,字段列表"中的未知列appname_usershow.show_id")
如您所见,我的模型中甚至不存在此列?为什么会出现这个错误?
As you see this column is not even present in my models? Why this error?
推荐答案
正如@inception 所说,我的表架构已更改 &运行 syncdb
没有更新已经创建的表.
As @inception said my tables schema had changed & running syncdb
did not update already created tables.
显然,通过 syncdb
更新时对模型的任何更改都不会更改(如更新/修改)实际表.所以我放弃了相关的 DB &在空数据库上运行 syncdb
.现在它工作正常.:)
Apparently any changes to the models when updated through syncdb
does not change (as in update/modify) the actual tables. So I dropped the relevant DB & ran syncdb
on empty DB. Now it works fine. :)
对其他人来说,SOUTH Django 的数据迁移工具似乎是最受欢迎的选项.它似乎提供了 django 模型和选项syncdb
不足.必须检查...
For others, SOUTH data migration tool for Django seems to be favorite option. It seems to provide options which django models & syncdb
falls short on. Must check out...
2019 年 9 月 29 日更新:从 Django 1.7 起,迁移已内置到 Django 的核心中.如果您运行的是以前较低版本的 Django,您可以在 BitBucket.
Update 29th Sept 2019: From Django 1.7 upwards, migrations are built into the core of Django. If you are running a previous lower version of Django, you can find the repository on BitBucket.
这篇关于Django 模型(1054,“'字段列表'中的未知列")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!