【问题标题】:Django (Python) : DatabaseError : x Table has No Column named yDjango(Python):DatabaseError:x表没有名为y的列
【发布时间】:2012-06-25 21:54:33
【问题描述】:

您好,我正在使用 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 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)

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

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 基本上是应用程序中名为EngineAccount

【问题讨论】:

  • 你忘记运行syncdb了吗?
  • @BurhanKhalid 谢谢,这对我来说太愚蠢了,但是它仍然给我一个 IntegrityError “列用户名不是唯一的” .. 当我没有在我的帐户中定义任何名为“用户名”的文件时模型。

标签: python database django sqlite


【解决方案1】:

您在运行 syncdb 后是否编辑了 models.py?

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

python manage.py syncdb

在项目目录中应用更改。


2019 年更新:syncdb 在 Django 1.9 及以后版本中已弃用。使用

python manage.py makemigrations <app_name>
python manage.py migrate --run-syncdb

2020 年更新:我想为我的答案添加更多描述。

如果模型和对应的表不匹配,就会出现这个问题。这意味着您已更改模型但未迁移更改。有些人不明白makemigrationsmigrate 命令之间的区别。为了更清楚,我将尝试解释其中的区别:

./manage.py makgemigrations &lt;app_name&gt;: 给定应用程序模型的变化将与之前的迁移进行比较。如果没有提供app_name,所有被跟踪的应用程序都将受到控制,如果有任何更改,将创建迁移。每个应用程序都有其迁移目录,此命令将在此目录中创建相关的迁移。创建迁移时,它对数据库没有任何更改,需要应用迁移。如果您不应用迁移,那么您仍然会收到相同的错误。

./manage.py migratemakemigrations 命令创建的迁移应用于 DB。在生产环境中迁移时需要小心。

./manage.py showmigrations:此命令列出迁移的状态,您可以查看是否应用了迁移。通常状态存储在数据库中,但不需要连接到数据库并爬取数据。此命令为您提供清晰的输入。

【讨论】:

    【解决方案2】:

    syncdb 在 django 1.9 及更高版本中已弃用。所以对于 django 1.9 或更高版本运行python manage.py makemigrations &lt;app-name&gt; 然后python manage.py migrate

    【讨论】:

      【解决方案3】:

      我试过python manage.py syncdb,还不够,所以解决办法是:

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

      我执行了python manage.py makemigrationspython manage.py migrate

      宾果游戏!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-30
        • 2020-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多