首先,我最近从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'")
答案 0 :(得分:8)
在导入模块或注册应用程序时,您似乎正在进行数据库查询:
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
。我想你可能需要在mymodel.MyManagerObjects.get()
而不是在类声明中调用MyListFilter.queryset()
。
https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter
答案 1 :(得分:0)
它不应该是:
python manage.py makemigrations mymodel
它应该是:
python manage.py makemigrations myapp
请注释掉new_integer_field
并重新做一遍,我想。
您还需要运行migrate
以在数据库表中添加new_integer_field
列。
python manage.py makemigrations myapp
./manage.py migrate --fake
# add new_integer_field
./manage.py makemigrations myapp
./manage.py migrate