python “django 视图中的未知列 'user_id' 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/293300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:50:03  来源:igfitidea点击:

"Unknown column 'user_id' error in django view

pythondjangodjango-modelsmodelview

提问by rksprst

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()

采纳答案by Ned Batchelder

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? Did you modify the SQL table structure?

您必须展示您的模型才能获得真正的帮助,但您的 Idea 表似乎没有 user_id 列?是否修改了 SQL 表结构?

回答by S.Lott

  1. The user_idfield is the FK reference from Ideato User. 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.

  2. Your model tables get an idfield by default. You can call it idin your queries. You can also use the synonym of pk.

    If you define your own primary key field you, you don't get the automatic idfield. But you can still use pkto refer to the Primary Key.

  1. user_id字段是从Idea到的 FK 引用User。看起来你已经改变了你的模型,没有更新你的数据库,那么你就会遇到这种问题。

    删除旧表,重新运行syncdb。

  2. 您的模型表id默认有一个字段。您可以id在查询中调用它。您也可以使用 的同义词pk

    如果您定义自己的主键字段,则不会获得自动id字段。但是您仍然可以使用pk来引用主键。

回答by Nick Orlowski

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

-尼克奥