简体   繁体   English

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

[英]"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()
  1. The user_id field is the FK reference from Idea to User . user_id字段是从IdeaUser的 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。

  2. 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.

相关问题 Django'列'user_id'不能为空' - Django 'Column 'user_id' cannot be null' Django:IntegrityError:列user_id不是唯一的 - Django: IntegrityError: column user_id is not unique 列 user_id 不是唯一的,具有 Django UserProfiles - column user_id is not unique, with Django UserProfiles Django OperationalError 表没有名为 user_id 的列 - Django OperationalError table has no column named user_id Django:(1048,“列'user_id'不能为null”) - Django: (1048, “Column 'user_id' cannot be null”) Django模型向导:IntegrityError列“ user_id”不能为空 - Django Model Wizard : IntegrityError Column “user_id” cannot be null 外键不起作用 - sqlite3.OperationalError:外键定义中的未知列“user_id” - Foreign keys not working - sqlite3.OperationalError: unknown column “user_id” in foreign key definition 注释 model 仅适用于用户 django(1048,“列 'user_id' 不能为空”或没有用户填写) - Comment model only for user django (1048, “Column 'user_id' cannot be null” or no user filled) MySQL使用Django的自动ID给出“字段列表中的未知列'user.id'”错误 - MySQL gives an “Unknown column 'user.id' in 'field list'” error using Django's automatic id Django/React/Postgres 一对多不会将 hero_id 列更新为 user_id - Django/React/Postgres One-to-Many wont update hero_id column to user_id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM