迁移后的 Django-cms 1.7.10 "OperationalError - no such column"

Django-cms 1.7.10 "OperationalError - no such column" after migration

所以我知道已经有很多人提出了很多问题,他们更改了模型然后未能将迁移应用到他们的数据库。但是,就我而言,我知道迁移已应用,因为我可以看到新的 table 数据。

基本上,我安装了 django-cms,然后我在 djangocms_column 插件的 models.py 中添加了一个字段,允许我添加一个 Bootstrap class 名称我的专栏(例如 col-md-4col-md-6 等)。

if hasattr(settings, "COLUMN_CLASS_CHOICES"):
    CLASS_CHOICES = settings.COLUMN_CLASS_CHOICES
else:
    CLASS_CHOICES = (
        ('col-md-1', _("col-md-1")),
        ('col-md-2', _("col-md-2")),
        ('col-md-3', _('col-md-3')),
        ('col-md-4', _("col-md-4")),
        ('col-md-5', _('col-md-5')),
        ('col-md-6', _("col-md-6")),
        ('col-md-7', _('col-md-7')),
        ('col-md-8', _('col-md-8')),
        ('col-md-9', _('col-md-9')),
        ('col-md-10', _('col-md-10')),
        ('col-md-11', _('col-md-11')),
        ('col-md-12', _('col-md-12')),
        ('', _('none')),
    )

...

@python_2_unicode_compatible
class Column(CMSPlugin):
    """
    A Column for the MultiColumns Plugin
    """

    width = models.CharField(_("width"), choices=WIDTH_CHOICES, default=WIDTH_CHOICES[0][0], max_length=50)

    """
    This is the new field:
    """
    bs_class = models.CharField(_("bs_class"), choices=CLASS_CHOICES, default=CLASS_CHOICES[0][0], max_length=50)

    def __str__(self):
        return u"%s" % self.get_width_display()

我然后 运行 ./manage.py makemigrations 然后 ./manage.py migrate,现在 table 看起来像这样:

sqlite> select * from djangocms_column_column;
cmsplugin_ptr_id  bs_class    width     
----------------  ----------  ----------
3                 col-md-1    33%       
5                 col-md-1    33%       
7                 col-md-1    33%       
19                col-md-1    33%       
21                col-md-1    33%       
23                col-md-1    33% 

但是当我尝试访问测试服务器时,仍然出现以下错误:

OperationalError at /en/
no such column: djangocms_column_column.bs_class
Request Method: GET
Request URL:    http://localhost:8000/en/
Django Version: 1.7.10
Exception Type: OperationalError
Exception Value:    
no such column: djangocms_column_column.bs_class

而且,是的,我已经尝试删除数据库和 运行 ./manage.py migrate,但该站点仍然显示相同的错误。是否必须使用一种特殊的迁移程序来修改 ./env/lib/python2.7/site-packages 文件夹中安装的插件?

您是否尝试过删除应用内迁移文件夹中的迁移?

所以我实际上弄清楚了导致这种行为的原因。在设计我的 gulp 任务时,我重组了项目文件夹,将我所有的 django 创建的文件放在 src 子目录中。

我这样做是因为当 gulpfile.jsbower_components 中的文件被修改时,以这种方式监视我的应用程序文件的更改会更容易,而不会无意中触发我的监视任务。 (最终,这并不重要,因为我的 glob 比 django 项目根目录更具体。)

这不会有问题,除非 settings.DATABASES['default']['NAME'] 是相对路径 project.db。结果,当我从 /src 目录中 运行 ./manage.py migrate 时,它在 /src/project.db 上执行了迁移。当我从父目录 运行 src/manage.py migrate 时,迁移是在 /project.db 上执行的。 djangocms 应用程序本身使用的是后者,而我一直在对前者执行所有迁移。

所以这里的教训是:

  • 确保您的 sqlite 文件是使用绝对路径指定的。
  • 当您遇到看似无法解释的迁移问题时,请检查以确保您的工作区中没有浮动多个 .db 文件。