ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Django(Python):DatabaseError:x表没有名为y的列

2019-07-23 10:07:34  阅读:239  来源: 互联网

标签:python django database sqlite3


嗨,我正在使用sqlite3数据库在django python应用程序上工作.我对我在models.py中定义的django用户模型进行了扩展,如下所示:

#Account Model
class Account(models.Model):
  user = models.OneToOneField(User)
  avatar_url = models.CharField(max_length=200)
  profile_url = models.CharField(max_length=200)
  account_type = models.CharField(max_length=60, choices=choices.ACCOUNT_TYPE)

我还有一个创建Account对象的方法和一个像这样定义的post_save处理程序:

#Function to Create user Account/Profile
def create_user_account(sender, instance, created, **kwargs):
  if created:
    models.Account.objects.create(user=instance)

#Create User / User Registration
def UserRegistration(request):
  if request.method == 'POST':
    username = request.POST['fn'].capitalize() + ' ' + request.POST['ln'].capitalize()
    # CREATE USER
    newuser = User.objects.create_user(username=username,
                                       email=request.POST['email'],
                                       password=request.POST['pw'])
    newuser.save()
  return HttpResponse(username)

#Post Save handler to create user Account/Profile
post_save.connect(create_user_account, sender=User)

现在,每当我尝试注册新用户时,我都会收到以下数据库错误:

DatabaseError at /register/

table engine_account has no column named user_id

Request Method:     POST
Request URL:    http://localhost:8000/register/
Django Version:     1.4
Exception Type:     DatabaseError
Exception Value:    

table engine_account has no column named user_id

Exception Location:     /usr/local/lib/python2.7/dist-packages/Django-1.4-py2.7.egg/django/db/backends/sqlite3/base.py in execute, line 337
Python Executable:  /usr/bin/python
Python Version:     2.7.3

我不知道“user_id”字段来自哪里..任何想法?

PS:

table engine_account基本上是名为Engine的应用程序中的Account类

解决方法:

你运行syncdb后编辑models.py了吗?

如果是这样,那么您应该手动编辑表或使用以下命令重新创建数据库:

python manage.py syncdb

在项目目录中应用更改.

标签:python,django,database,sqlite3
来源: https://codeday.me/bug/20190723/1512556.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有