Django:"Unknown Column" when 运行 makemigrations on an app after adding an Integer Field
Django: "Unknown Column" when run makemigrations on an app after adding an Integer Field
首先,我最近从 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:我添加了回溯的开始...我讨厌 post 任何实际的 paths/model 名称,因此进行编辑。希望没关系。注意 - 据我所知,它与 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'")
不应该是:
python manage.py makemigrations mymodel
应该是:
python manage.py makemigrations myapp
所以注释掉 new_integer_field
我想再做一次。
您还需要 运行 migrate
在数据库中添加 new_integer_field
列 table.
python manage.py makemigrations myapp
./manage.py migrate --fake
# add new_integer_field
./manage.py makemigrations myapp
./manage.py migrate
您似乎在导入模块或注册应用程序时进行了数据库查询:
curstart = mymodel.MyManagerObjects.get().old_field
这意味着当您 运行 任何管理命令如 runserver
、makemigrations
或 migrate
时,如果您更改了模型,那么它们将不同步使用您的数据库,并且在命令可以执行它应该执行的操作(例如进行迁移)之前进行查询将抛出异常。
如果可能,请修改您的代码,以便 MyManagerObjects.get()
不会在加载时调用,例如,通过使用 lambda 函数或将其包装在您可以在 运行 时调用的方法中。
如果这不可能,您需要在每次进行或 运行 迁移时注释掉该部分代码,但这真的很麻烦。
更新:
在完整的回溯中有一行:
File ".../myapp/admin.py", line 154, in MyListFilter
x = mymodel.MyManagerObjects.get()
这意味着在导入管理文件时 get
正在 运行。我认为您可能需要在 MyListFilter.queryset()
中而不是在 class 声明中调用 mymodel.MyManagerObjects.get()
。
https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter
首先,我最近从 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:我添加了回溯的开始...我讨厌 post 任何实际的 paths/model 名称,因此进行编辑。希望没关系。注意 - 据我所知,它与 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'")
不应该是:
python manage.py makemigrations mymodel
应该是:
python manage.py makemigrations myapp
所以注释掉 new_integer_field
我想再做一次。
您还需要 运行 migrate
在数据库中添加 new_integer_field
列 table.
python manage.py makemigrations myapp
./manage.py migrate --fake
# add new_integer_field
./manage.py makemigrations myapp
./manage.py migrate
您似乎在导入模块或注册应用程序时进行了数据库查询:
curstart = mymodel.MyManagerObjects.get().old_field
这意味着当您 运行 任何管理命令如 runserver
、makemigrations
或 migrate
时,如果您更改了模型,那么它们将不同步使用您的数据库,并且在命令可以执行它应该执行的操作(例如进行迁移)之前进行查询将抛出异常。
如果可能,请修改您的代码,以便 MyManagerObjects.get()
不会在加载时调用,例如,通过使用 lambda 函数或将其包装在您可以在 运行 时调用的方法中。
如果这不可能,您需要在每次进行或 运行 迁移时注释掉该部分代码,但这真的很麻烦。
更新: 在完整的回溯中有一行:
File ".../myapp/admin.py", line 154, in MyListFilter
x = mymodel.MyManagerObjects.get()
这意味着在导入管理文件时 get
正在 运行。我认为您可能需要在 MyListFilter.queryset()
中而不是在 class 声明中调用 mymodel.MyManagerObjects.get()
。
https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter