postgresql 列< column>不存在(Django 1.8)

gzjq41n4  于 5个月前  发布在  PostgreSQL
关注(0)|答案(7)|浏览(65)

我正在尝试与我的django项目的开发服务器进行交互。然而,服务器上的任何页面都返回相同的错误:

Exception Type: ProgrammingError

Exception Value: column myApp_verb.date does not exist

字符串
我最近没有将字段日期添加到模型动词中(它已经存在了一段时间,我不确定是什么导致了这个错误开始)。我的协作者在他们的本地机器上都有相同的文件,他们都没有任何问题。
我尝试了各种各样的东西:
我已经尝试删除日期字段(以及对它的所有引用)。makemigrations没有检测到任何更改,migrate失败并出现错误:
django.db.utils.ProgrammingError: column "date" does not exist
我已经尝试重命名字段。makemigrations再次没有检测到任何更改,并且迁移失败,错误与上面相同。
我已经尝试删除所有的迁移。这并没有改变什么。
我也没办法了。如果能帮上忙我会很感激的。
先谢谢你了!
编辑:这里是动词类,按照要求。它很简单:

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)

2exbekwf

2exbekwf1#

您可以创建手动迁移来解决此问题。
首先注解掉抛出错误的列。
然后编写手动迁移。通常是这样的:

# -*- 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字段。
然后,取消注解导致错误的字段。
对我来说就像一个魅力。

rjjhvcjd

rjjhvcjd2#

我仍然不知道为什么会出现这个错误,但看起来我的数据库中有某种损坏。我停用了数据库并启动了一个新的数据库,一切都再次完美地工作。

jobtbby3

jobtbby33#

有同样的问题。我想添加字段“鼻涕虫”到城市模型。
当我用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'):

c6ubokkw

c6ubokkw4#

完全同意Özer S的观点。是的,这是克服这个问题的唯一解决方案。亲爱的Django开发人员,为什么当试图向现有表添加字段时,Django突然回应说这样的字段不存在?当然不存在,所以我正在尝试添加它!
同样的解决方案也适用于我的情况-添加一个带有多项选择的字段:
1.这就是models.py中添加的字段的样子:

TEMPLATE = (
  ('list', 'List'), ('table', 'Table')
)
template = models.CharField(
    'View template',
    choices=TEMPLATE,
    max_length=7,
    default='list'
)

字符串
1.手动创建一个迁移文件,使用上一次迁移后的编号。我将“template”字段添加到“blog”应用程序中的“blogcategory”表中,我的迁移文件名为“0005_template”。
1.下面是迁移文件(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'
        ),
    ),
]


1.接下来,对模型中的这些行进行注解:

TEMPLATE = (
  ('list', 'List'), ('table', 'Table')
)
template = models.CharField(
   'View template',
   choices=TEMPLATE,
   max_length=7,
   default='list'
)


1.然后,在数据库中执行应用程序迁移:

python manage.py migrate blog


并获得

Operations to perform:
    Apply all migrations: blog
Running migrations:
    Applying blog.0005_template... OK


因此,默认为“list”的“template”字段被添加到“blogcategory”表中的所有条目中。
P.S.不要忘记取消注解模型中的字段。

t8e9dugd

t8e9dugd5#

我遇到过这个问题。我通过在数据库中的Django migrations表中找到错误的迁移来修复它。在我的情况下,这是一个相当新的迁移。我删除了这个迁移和其他跟随它的迁移。然后重试运行python manage.py makemigrationspython manage.py migrate。这次两个命令都运行了,错误消失了。

gxwragnw

gxwragnw6#

你通常可以通过“欺骗”django在生成迁移时认为列存在来避免这种类型的错误。

  • 在本地数据库中手动创建同名的新数据库列
  • 运行python manage.py makemigrations以生成正确的迁移
  • 删除之前创建的列
  • 运行python manage.py migrate 'appname'创建新列
sg2wtvxw

sg2wtvxw7#

在我的情况下,我得到了这个错误,当我第一次使用模型.ForeignKey()然后改为OneToOneFieldf().我只是删除数据库,然后创建一个全新的,运行迁移和它的工作

相关问题