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

问题描述 投票:6回答:2

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

我跑了:

> python manage.py makemigrations myapp
> python manage.py migrate --fake initial

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

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

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

现在我跑的时候:

>python manage.py makemigrations myapp

我明白了

django.db.utils.OperationalError: (1054, "Unknown column 'myapp_mymodel.new_integer_field' in 'field list'")

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

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'")

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.

在myapp / models.py里面:

MyManagerObjects = MyManager()

所以,我的问题是,为什么我不能在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'")
mysql django python-2.7 django-migrations django-managers
2个回答
8
投票

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

这意味着当您运行任何管理命令(如runservermakemigrationsmigrate)时,如果您更改了模型,那么它们将与您的数据库不同步,并且在命令可以执行它应该执行的操作之前使查询抛出异常(比如进行迁移)。

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

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

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

File ".../myapp/admin.py", line 154, in MyListFilter
    x = mymodel.MyManagerObjects.get()

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

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


0
投票

它不应该是:

python manage.py makemigrations mymodel

它应该是:

python manage.py makemigrations myapp

因此,请注释new_integer_field,我想再做一次。

您还需要运行migrate以在数据库表中添加new_integer_fieldcolumn。

python manage.py makemigrations myapp
./manage.py migrate --fake
# add new_integer_field
./manage.py makemigrations myapp
./manage.py migrate
© www.soinside.com 2019 - 2024. All rights reserved.