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

Amy*_*yth 3 python database sqlite django

嗨,我正在使用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)
Run Code Online (Sandbox Code Playgroud)

我还有一个方法来创建Account object和这样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)
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

我不知道那个"user_id"领域来自哪里......任何想法?

PS:

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

scr*_*ter 7

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

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

python manage.py syncdb
Run Code Online (Sandbox Code Playgroud)

在项目目录中应用更改.


Jun*_*aid 6

syncdb在django 1.9和更高版本中已弃用。所以Django的版本1.9或更高版本上运行python manage.py makemigrations <app-name>,然后python manage.py migrate


小智 5

我尝试过 python manage.py syncdb,但这还不够,所以解决方案是:

我删除了 migrations file,我删除了 db.sqlite3文件,

我执行了python manage.py makemigrations并且还 python manage.py migrate

答对了 !