【问题标题】:column <column> does not exist (Django 1.8)<column> 列不存在(Django 1.8)
【发布时间】:2015-09-16 02:58:33
【问题描述】:

我正在尝试与我的 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)

【问题讨论】:

  • 您需要分享一些代码以获得帮助。
  • 我可以看看verb的型号吗?
  • @NightShadeQueen 我已经添加了

标签: python django postgresql django-models django-migrations


【解决方案1】:

我仍然不知道为什么会出现此错误,但我的数据库中似乎存在某种损坏。我停用了数据库并启动了一个新数据库,一切都再次完美运行。

【讨论】:

    【解决方案2】:

    您可以创建手动迁移来解决此问题。

    首先注释掉抛出错误的列。

    然后编写手动迁移。通常是这样的:

    # -*- 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。它将创建那个/那些字段。

    然后,取消注释导致错误的字段。

    像魅力一样为我工作。

    【讨论】:

      【解决方案3】:

      有同样的问题。我想在 City 模型中添加字段“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'):
      

      【讨论】:

        【解决方案4】:

        完全同意 Özer S。 是的,这是克服这个问题的唯一解决方案。亲爱的 Django 开发者,为什么在尝试向现有表添加字段时,Django 突然响应说这样的字段不存在?当然不存在,所以我正在尝试添加它!

        同样的解决方案适用于我的情况 - 添加一个具有多项选择的字段:

        1. 这是 models.py 中添加的字段的样子:

          TEMPLATE = (
            ('list', 'List'), ('table', 'Table')
          )
          template = models.CharField(
              'View template',
              choices=TEMPLATE,
              max_length=7,
              default='list'
          ) 
          
        2. 手动创建一个迁移文件,其编号跟在上一次迁移之后。我将“模板”字段添加到“博客”应用程序的“博客类别”表中,我的迁移文件名为“0005_template”。

        3. 这是迁移文件(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'
                  ),
              ),
          ]
          
        4. 接下来,对模型中的这些行进行注释:

          TEMPLATE = (
            ('list', 'List'), ('table', 'Table')
          )
          template = models.CharField(
             'View template',
             choices=TEMPLATE,
             max_length=7,
             default='list'
          )
          
        5. 然后,在数据库中做应用迁移:

          python manage.py migrate blog
          

          得到

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

        因此,带有默认“列表”的“模板”字段被添加到“blogcategory”表中的所有条目中。

        附:不要忘记取消注释模型中的字段。

        【讨论】:

          【解决方案5】:

          我遇到过这个问题。我通过转到数据库中的 Django 迁移表并找到错误的迁移来修复它。就我而言,这是一个相当近期的迁移。我删除了该迁移以及跟随它的其他迁移。然后重试运行python manage.py makemigrationspython manage.py migrate。这次两个命令都运行并且错误消失了。

          【讨论】:

            【解决方案6】:

            您通常可以通过“欺骗”django 使其在生成迁移时认为该列存在来绕过此类错误。

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

            【讨论】:

              猜你喜欢
              • 2015-12-13
              • 1970-01-01
              • 2015-10-17
              • 2015-11-18
              • 2015-08-14
              • 2023-03-19
              • 2018-03-25
              • 2015-07-17
              • 1970-01-01
              相关资源
              最近更新 更多