Django:在添加整数字段后在应用程序上运行makemigrations时出现"未知列"

jen*_*ren 6 mysql django django-managers python-2.7 django-migrations

首先,我最近从Django 1.6升级到1.8,从未使用过South,因此我不熟悉迁移.

我跑了:

> python manage.py makemigrations myapp
> python manage.py migrate --fake initial
Run Code Online (Sandbox Code Playgroud)

使用现有MySQL表为我的模型创建初始迁移.到目前为止一切似乎都很好

然后我在我的模型中添加了一个IntegerField:

new_integer_field = models.IntegerField(default = 50) #can include blank=True, null=True; seems to make no difference
Run Code Online (Sandbox Code Playgroud)

现在我跑的时候:

>python manage.py makemigrations myapp
Run Code Online (Sandbox Code Playgroud)

我明白了

django.db.utils.OperationalError: (1054, "Unknown column 'myapp_mymodel.new_integer_field' in 'field list'")
Run Code Online (Sandbox Code Playgroud)

追溯(从问题发生的地方开始)是:

Traceback (most recent call last):
    File "./manage.py", line 10, in <module>
        execute_from_command_line(sys.argv)
    File ".../django/core/management/__init__.py", line 338, in execute_from_command_line
        utility.execute()
    File ".../django/core/management/__init__.py", line 312, in execute
        django.setup()
    File ".../django/__init__.py", line 18, in setup
        apps.populate(settings.INSTALLED_APPS)
    File ".../django/apps/registry.py", line 115, in populate
        app_config.ready()
    File ".../autocomplete_light/apps.py", line 9, in ready
        autodiscover()
    File ".../autocomplete_light/registry.py", line 298, in autodiscover
        autodiscover_modules('autocomplete_light_registry')
    File ".../django/utils/module_loading.py", line 74, in autodiscover_modules
        import_module('%s.%s' % (app_config.name, module_to_search))
    File ".../python2.7/importlib/__init__.py", line 37, in import_module
        __import__(name)
    File ".../mymapp/autocomplete_light_registry.py", line 42, in <module>
        x = other_model.other_model_manager.manager_function(),
    File ".../myapp/models.py", line 759, in get_a_queryset
        stus = a_queryset
    File ".../myapp/models.py", line 92, in get_another_queryset
        if obj.model_function(prog):
    File ".../myapp/models.py", line 402, in model_function
        z = object.MyManagerObjects.manager_function(self)
    File ".../myapp/models.py", line 573, in get_type
        curstart = mymodel.MyManagerObjects.get().old_field
    File ".../python2.7/site-packages/django/db/models/manager.py", line 127, in manager_method
        return getattr(self.get_queryset(), name)(*args, **kwargs)
    File ".../python2.7/site-packages/django/db/models/query.py", line 328, in get
        num = len(clone)
    File ".../python2.7/site-packages/django/db/models/query.py", line 144, in __len__
        self._fetch_all()
    File ".../python2.7/site-packages/django/db/models/query.py", line 965, in _fetch_all
        self._result_cache = list(self.iterator())
    File ".../python2.7/site-packages/django/db/models/query.py", line 238, in iterator
        results = compiler.execute_sql()
    File ".../python2.7/site-packages/django/db/models/sql/compiler.py", line 840, in execute_sql
        cursor.execute(sql, params)
    File ".../python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
        return super(CursorDebugWrapper, self).execute(sql, params)
    File ".../python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
        return self.cursor.execute(sql, params)
    File ".../python2.7/site-packages/django/db/utils.py", line 97, in __exit__
        six.reraise(dj_exc_type, dj_exc_value, traceback)
    File ".../python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
        return self.cursor.execute(sql, params)
    File ".../python2.7/site-packages/django/db/backends/mysql/base.py", line 124, in execute
        return self.cursor.execute(query, args)
    File ".../python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
        self.errorhandler(self, exc, value)
    File ".../python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
        raise errorclass, errorvalue
django.db.utils.OperationalError: (1054, "Unknown column 'myapp_mymodel.new_integer_field' in 'field list'")
Run Code Online (Sandbox Code Playgroud)

MyManager很简单,如下:

class MyManager(models.Manager):
    def get_queryset(self):
        return super(MyManager, self).get_queryset().filter(...some filters...) #this is guaranteed to return a queryset with one object in it.
Run Code Online (Sandbox Code Playgroud)

在myapp/models.py里面:

MyManagerObjects = MyManager()
Run Code Online (Sandbox Code Playgroud)

所以,我的问题是,为什么我不能在mymodel中添加一个字段并运行makemigrations?什么打破经理?我正在那里开始堆栈跟踪,因为我尝试了各种不同的评论策略,这些策略似乎指向这位经理而不管之前的电话是什么.

我肯定希望有人可以告诉我这是一件快速而简单的事情,我只是没有看到,但我不确定情况会是怎样的!其他人有这个麻烦或有任何建议吗?

----****----更新1:错误不仅仅发生在makemigrations上 - 因为一些评论员询问我跑的时候发生了什么python manage.py migrate我以为我会尝试它来做事情和咯咯笑,即使有什么都没有迁移.我本来希望被告知没有什么可以迁移,但我得到了同样的错误.所以这不是makemigrations代码本身; 它无法正常运行,因为它找不到新的字段.不知何故,它正在查看模型并找到一个它无法识别的字段.但为什么?!

----****----更新2:我添加了追溯的开始...我不愿发布任何实际路径/模型名称,因此编辑.希望没关系.注意 - 据我所知,它不是与autocomplete_light有关(除非它是!),因为当我注释掉行x = other_model.other_model_manager.manager_function()(autocomplete_light_registry.py中的第42行)时,会发生错误而不引用autocomplete_light.我在这里添加了第二个回溯以防万一也有帮助!

Traceback (most recent call last):
    File "./manage.py", line 10, in <module>
        execute_from_command_line(sys.argv)
    File ".../django/core/management/__init__.py", line 338, in execute_from_command_line
        utility.execute()
    File ".../django/core/management/__init__.py", line 312, in execute
        django.setup()
    File ".../django/__init__.py", line 18, in setup
        apps.populate(settings.INSTALLED_APPS)
    File ".../django/apps/registry.py", line 115, in populate
        app_config.ready()
    File ".../django/contrib/admin/apps.py", line 22, in ready
        self.module.autodiscover()
    File ".../django/contrib/admin/__init__.py", line 24, in autodiscover
        autodiscover_modules('admin', register_to=site)
    File ".../django/utils/module_loading.py", line 74, in autodiscover_modules
        import_module('%s.%s' % (app_config.name, module_to_search))
    File ".../python2.7/importlib/__init__.py", line 37, in import_module
        __import__(name)
    File ".../myapp/admin.py", line 151, in <module>
        class MyListFilter(SimpleListFilterWithDefault):
    File ".../myapp/admin.py", line 154, in MyListFilter
        x = mymodel.MyManagerObjects.get()
    File ".../django/db/models/manager.py", line 127, in manager_method
        return getattr(self.get_queryset(), name)(*args, **kwargs)
    File ".../django/db/models/query.py", line 328, in get
        num = len(clone)
    File ".../django/db/models/query.py", line 144, in __len__
        self._fetch_all()
    File ".../django/db/models/query.py", line 965, in _fetch_all
        self._result_cache = list(self.iterator())
    File ".../django/db/models/query.py", line 238, in iterator
        results = compiler.execute_sql()
    File ".../django/db/models/sql/compiler.py", line 840, in execute_sql
        cursor.execute(sql, params)
    File ".../django/db/backends/utils.py", line 79, in execute
        return super(CursorDebugWrapper, self).execute(sql, params)
    File ".../django/db/backends/utils.py", line 64, in execute
        return self.cursor.execute(sql, params)
    File ".../django/db/utils.py", line 97, in __exit__
        six.reraise(dj_exc_type, dj_exc_value, traceback)
    File ".../django/db/backends/utils.py", line 64, in execute
        return self.cursor.execute(sql, params)
    File ".../django/db/backends/mysql/base.py", line 124, in execute
        return self.cursor.execute(query, args)
    File ".../MySQLdb/cursors.py", line 205, in execute
        self.errorhandler(self, exc, value)
    File ".../MySQLdb/connections.py", line 36, in defaulterrorhandler
        raise errorclass, errorvalue
django.db.utils.OperationalError: (1054, "Unknown column 'myapp_mymodel.new_integer_field' in 'field list'")
Run Code Online (Sandbox Code Playgroud)

Fus*_*ush 8

在导入模块或注册应用程序时,您似乎正在进行数据库查询: curstart = mymodel.MyManagerObjects.get().old_field

这意味着当您运行任何管理命令时runserver,makemigrations或者migrate,如果您更改了模型,那么它们将与您的数据库不同步,并使查询在命令可以执行预期之前抛出异常(如make你的迁移).

如果可能,修改代码,以便MyManagerObjects.get()在加载时不调用,例如,使用lambda函数或将其包装在可在运行时调用的方法中.

如果那是不可能的,那么每次进行或运行迁移时,您都需要注释掉该部分代码,但这确实很混乱.

更新:在完整的追溯中有一条线:

File ".../myapp/admin.py", line 154, in MyListFilter
    x = mymodel.MyManagerObjects.get()
Run Code Online (Sandbox Code Playgroud)

这意味着在get导入管理文件时正在运行.我想你可能需要把调用放在mymodel.MyManagerObjects.get()in MyListFilter.queryset()而不是在类声明中.

https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter