[英]"Unknown column 'user_id' error in django view
I'm having an error where I am not sure what caused it.我遇到了一个错误,我不确定是什么原因造成的。
Here is the error:这是错误:
Exception Type: OperationalError
Exception Value:
(1054, "Unknown column 'user_id' in 'field list'")
Does anyone know why I am getting this error?有谁知道为什么我会收到这个错误? I can't figure it out.我想不通。 Everything seems to be fine.一切似乎都很好。
My view code is below:我的视图代码如下:
if "login" in request.session:
t = request.POST.get('title', '')
d = request.POST.get('description', '')
fid = request.session["login"]
fuser = User.objects.get(id=fid)
i = Idea(user=fuser, title=t, description=d, num_votes=1)
i.save()
return HttpResponse("true", mimetype="text/plain")
else:
return HttpResponse("false", mimetype="text/plain")
I appreciate any help!我感谢任何帮助! Thanks!谢谢!
Edit: Also a side question.编辑:也是一个附带问题。 Do I use objects.get(id= or objects.get(pk= ? If I use a primary key, do I need to declare an id field or an index in the model?我是否使用 objects.get(id= 或 objects.get(pk= ?如果我使用主键,我是否需要在模型中声明一个 id 字段或索引?
Edit: Here are the relevant models:编辑:以下是相关模型:
class User (models.Model):
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
email = models.CharField(max_length=200)
password = models.CharField(max_length=200)
class Idea (models.Model):
user = models.ForeignKey(User)
title = models.CharField(max_length=200)
description = models.CharField(max_length=255)
num_votes = models.IntegerField()
The user_id
field is the FK reference from Idea
to User
. user_id
字段是从Idea
到User
的 FK 引用。 It looks like you've changed your model, and not updated your database, then you'll have this kind of problem.看起来你已经改变了你的模型,而不是更新你的数据库,那么你就会遇到这种问题。
Drop the old table, rerun syncdb.删除旧表,重新运行syncdb。
Your model tables get an id
field by default.默认情况下,您的模型表获得一个id
字段。 You can call it id
in your queries.您可以在查询中将其称为id
。 You can also use the synonym of pk
.您也可以使用pk
的同义词。
If you define your own primary key field you, you don't get the automatic id
field.如果您定义自己的主键字段,则不会获得自动id
字段。 But you can still use pk
to refer to the Primary Key.但是您仍然可以使用pk
来引用主键。
You'll have to show your models to get real help, but it looks like your Idea table doesn't have a user_id column?您必须展示您的模型才能获得真正的帮助,但您的 Idea 表似乎没有 user_id 列? Did you modify the SQL table structure?您是否修改了 SQL 表结构?
Yes, I dropped the tables and it all worked great.是的,我放下了桌子,一切都很好。 However, you have to actually go into the database and DROP them.但是,您必须实际进入数据库并删除它们。 "manage.py flush" or "manage.py reset appname" won't do it by themselves. “manage.py flush”或“manage.py reset appname”不会自己完成。
-Nick O -尼克奥
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.