列 <column> 不存在(Django 1.8)
column <column> does not exist (Django 1.8)
我正在尝试与我的 Django 项目的开发服务器进行交互。但是服务器上的任何页面 returns 同样的错误:
Exception Type: ProgrammingError
Exception Value: column myApp_verb.date does not exist
我最近没有将字段日期添加到模型动词中(它已经存在了一段时间,但我不确定是什么原因导致此错误开始的)。我的合作者在他们的本地机器上都有相同的文件,其中 none 有任何问题。
我尝试过各种方法:
我试过删除日期字段(以及所有对它的引用)。 makemigrations
未检测到任何更改,migrate
失败并出现错误:
django.db.utils.ProgrammingError: column "date" does not exist
我试过重命名字段。再次 makemigrations
没有检测到任何更改,迁移失败并出现与上述相同的错误。
我已尝试删除所有迁移。这没有任何改变。
我现在没主意了。任何帮助将不胜感激。
提前致谢!
编辑:根据要求,这是动词 class。非常简单:
class Verb(models.Model):
english_gloss = models.CharField(max_length = 20)
first_person = models.CharField(max_length = 20)
second_person = models.CharField(max_length = 20)
third_person = models.CharField(max_length = 20)
fourth_person = models.CharField(max_length = 20)
transitivity = models.BooleanField()
classifier = models.CharField(max_length = 20)
inner_lexical = models.CharField(max_length = 20)
outer_lexical = models.CharField(max_length = 20)
perfective = models.CharField(max_length = 20)
imperfective = models.CharField(max_length = 20)
date = models.DateTimeField(auto_now_add = True)
我仍然不知道为什么会出现此错误,但看来我的数据库中存在某种损坏。我停用了数据库并启动了一个新数据库,一切又恢复正常了。
您可以创建手动迁移来解决此问题。
首先注释掉出现错误的列。
然后写手动迁移。通常是这样的:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('my_field', 'last_migration_filename'), # no .py
]
operations = [
migrations.AddField(
model_name='my_model',
name='my_field',
field=models.MyField(blank=True, null=True),
),
]
然后运行python manage.py migrate
。它将创建 that/those 个字段。
然后,取消注释导致错误的字段。
对我很有帮助。
有同样的问题。我想将字段 "slug" 添加到城市模型。
当我用 select_related('city') 临时注释行时错误消失了。
for location in Location.objects.filter().select_related('city'):
cities[str(l.id)] = l.city.id
堆栈跟踪让我看到了代码的和平:
File "/www/loorn/apps/users/widgets.py", line 69, in LocationSelect
for location in Location.objects.filter().select_related('city'):
完全同意 Özer S。
是的,这是克服此问题的唯一解决方案。亲爱的 Django 开发者,为什么在尝试向现有 table 添加字段时,Django 突然响应说这样的字段不存在?当然是不存在的,所以我正在尝试添加它!
同样的解决方案适用于我的情况 - 添加一个具有多项选择的字段:
这是 models.py 中添加的字段的样子:
TEMPLATE = (
('list', 'List'), ('table', 'Table')
)
template = models.CharField(
'View template',
choices=TEMPLATE,
max_length=7,
default='list'
)
手动创建一个迁移文件,编号跟在上一次迁移之后。我将 "template" 字段添加到 "blog" 应用程序中的 "blogcategory" table,我的迁移文件名为“0005_template”。
这是迁移文件的内容 (0005_template.py):
# -*- coding: utf-8 -*-
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('blog', '0004_auto_20170628_1533'),
]
operations = [
migrations.AddField(
model_name='blogcategory',
name='template',
field=models.CharField(
verbose_name='View template',
choices=[('list', 'List'), ('table', 'Table')],
max_length=7,
default='list'
),
),
]
接下来,对模型中的这些行进行注释:
TEMPLATE = (
('list', 'List'), ('table', 'Table')
)
template = models.CharField(
'View template',
choices=TEMPLATE,
max_length=7,
default='list'
)
然后,进行数据库中的应用迁移:
python manage.py migrate blog
并得到
Operations to perform:
Apply all migrations: blog
Running migrations:
Applying blog.0005_template... OK
因此,具有默认值 "list" 的 "template" 字段被添加到 "blogcategory" table 中的所有条目。
P.S。不要忘记取消注释模型中的字段。
我遇到过这个问题。我通过转到数据库中的 Django 迁移 table 并找到有问题的迁移来修复它。就我而言,这是最近的一次迁移。我删除了那个迁移和它后面的其他迁移。然后重试 运行 python manage.py makemigrations
和 python manage.py migrate
。这次两个命令 运行 错误都消失了。
您通常可以通过 "tricking" django 在生成迁移时认为该列存在来绕过此类错误。
- 在本地数据库中手动创建同名的新数据库列
- 运行
python manage.py makemigrations
生成正确的迁移
- 删除您之前创建的列
- 运行
python manage.py migrate 'appname'
创建新列
我正在尝试与我的 Django 项目的开发服务器进行交互。但是服务器上的任何页面 returns 同样的错误:
Exception Type: ProgrammingError
Exception Value: column myApp_verb.date does not exist
我最近没有将字段日期添加到模型动词中(它已经存在了一段时间,但我不确定是什么原因导致此错误开始的)。我的合作者在他们的本地机器上都有相同的文件,其中 none 有任何问题。
我尝试过各种方法:
我试过删除日期字段(以及所有对它的引用)。 makemigrations
未检测到任何更改,migrate
失败并出现错误:
django.db.utils.ProgrammingError: column "date" does not exist
我试过重命名字段。再次 makemigrations
没有检测到任何更改,迁移失败并出现与上述相同的错误。
我已尝试删除所有迁移。这没有任何改变。
我现在没主意了。任何帮助将不胜感激。
提前致谢!
编辑:根据要求,这是动词 class。非常简单:
class Verb(models.Model):
english_gloss = models.CharField(max_length = 20)
first_person = models.CharField(max_length = 20)
second_person = models.CharField(max_length = 20)
third_person = models.CharField(max_length = 20)
fourth_person = models.CharField(max_length = 20)
transitivity = models.BooleanField()
classifier = models.CharField(max_length = 20)
inner_lexical = models.CharField(max_length = 20)
outer_lexical = models.CharField(max_length = 20)
perfective = models.CharField(max_length = 20)
imperfective = models.CharField(max_length = 20)
date = models.DateTimeField(auto_now_add = True)
我仍然不知道为什么会出现此错误,但看来我的数据库中存在某种损坏。我停用了数据库并启动了一个新数据库,一切又恢复正常了。
您可以创建手动迁移来解决此问题。
首先注释掉出现错误的列。
然后写手动迁移。通常是这样的:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('my_field', 'last_migration_filename'), # no .py
]
operations = [
migrations.AddField(
model_name='my_model',
name='my_field',
field=models.MyField(blank=True, null=True),
),
]
然后运行python manage.py migrate
。它将创建 that/those 个字段。
然后,取消注释导致错误的字段。
对我很有帮助。
有同样的问题。我想将字段 "slug" 添加到城市模型。
当我用 select_related('city') 临时注释行时错误消失了。
for location in Location.objects.filter().select_related('city'):
cities[str(l.id)] = l.city.id
堆栈跟踪让我看到了代码的和平:
File "/www/loorn/apps/users/widgets.py", line 69, in LocationSelect
for location in Location.objects.filter().select_related('city'):
完全同意 Özer S。 是的,这是克服此问题的唯一解决方案。亲爱的 Django 开发者,为什么在尝试向现有 table 添加字段时,Django 突然响应说这样的字段不存在?当然是不存在的,所以我正在尝试添加它!
同样的解决方案适用于我的情况 - 添加一个具有多项选择的字段:
这是 models.py 中添加的字段的样子:
TEMPLATE = ( ('list', 'List'), ('table', 'Table') ) template = models.CharField( 'View template', choices=TEMPLATE, max_length=7, default='list' )
手动创建一个迁移文件,编号跟在上一次迁移之后。我将 "template" 字段添加到 "blog" 应用程序中的 "blogcategory" table,我的迁移文件名为“0005_template”。
这是迁移文件的内容 (0005_template.py):
# -*- coding: utf-8 -*- from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ('blog', '0004_auto_20170628_1533'), ] operations = [ migrations.AddField( model_name='blogcategory', name='template', field=models.CharField( verbose_name='View template', choices=[('list', 'List'), ('table', 'Table')], max_length=7, default='list' ), ), ]
接下来,对模型中的这些行进行注释:
TEMPLATE = ( ('list', 'List'), ('table', 'Table') ) template = models.CharField( 'View template', choices=TEMPLATE, max_length=7, default='list' )
然后,进行数据库中的应用迁移:
python manage.py migrate blog
并得到
Operations to perform: Apply all migrations: blog Running migrations: Applying blog.0005_template... OK
因此,具有默认值 "list" 的 "template" 字段被添加到 "blogcategory" table 中的所有条目。
P.S。不要忘记取消注释模型中的字段。
我遇到过这个问题。我通过转到数据库中的 Django 迁移 table 并找到有问题的迁移来修复它。就我而言,这是最近的一次迁移。我删除了那个迁移和它后面的其他迁移。然后重试 运行 python manage.py makemigrations
和 python manage.py migrate
。这次两个命令 运行 错误都消失了。
您通常可以通过 "tricking" django 在生成迁移时认为该列存在来绕过此类错误。
- 在本地数据库中手动创建同名的新数据库列
- 运行
python manage.py makemigrations
生成正确的迁移 - 删除您之前创建的列
- 运行
python manage.py migrate 'appname'
创建新列