提问人:Joe Davy 提问时间:10/2/2017 最后编辑:Joe Davy 更新时间:10/2/2017 访问量:448
Django 迁移失败,使用新数据库
Django migrations failing with fresh database
问:
想知道是否有其他人在 Django 迁移中遇到令人沮丧的行为。我查看了 SO 上的几篇相关帖子,但没有找到任何专门针对此问题的内容。
这种情况不断发生在我身上,这一定是一个常见的场景,所以我错过了什么?
我的场景:
- 列表项
- 我已经构建了一个 Django 应用程序,在我的本地机器上使用 sqlite 进行迭代
- 我现在想部署到生产 Postgresql 数据库(AWS 或其他地方)
- 我设置了我的新数据库,将其添加到我的设置中,然后运行
python manage.py migrate
- 我收到一个 SQL 错误,告诉我我的一个模型表不存在。咄!这就是我运行迁移的原因。
- 我最终不得不手工制作桌子,做一些奇怪的事情,比如这会给像这样的软件包带来进一步的问题
python manage.py migrate <app_name> --fake
simple_history
我相信一定有一个显而易见的答案 - 我应该怎么做才能在全新的、干净的数据库上设置数据库模式?
编辑:我收到的错误回溯:
当我在空白数据库上运行时,出现以下错误:python manage.py migrate
(banzai-platform-django) ➜ src git:(master) ✗ ./manage.py migrate
Traceback (most recent call last):
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "events_event" does not exist
LINE 1: ...ents_event"."created", "events_event"."slug" FROM "events_ev...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "./manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/site-packages/django/core/management/base.py", line 327, in execute
self.check()
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/site-packages/django/core/management/base.py", line 359, in check
include_deployment_checks=include_deployment_checks,
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 62, in _run_checks
issues.extend(super(Command, self)._run_checks(**kwargs))
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/site-packages/django/core/management/base.py", line 346, in _run_checks
return checks.run_checks(**kwargs)
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/site-packages/django/core/checks/urls.py", line 16, in check_url_config
return check_resolver(resolver)
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/site-packages/django/core/checks/urls.py", line 26, in check_resolver
return check_method()
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/site-packages/django/urls/resolvers.py", line 254, in check
for pattern in self.url_patterns:
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/site-packages/django/urls/resolvers.py", line 405, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/site-packages/django/urls/resolvers.py", line 398, in urlconf_module
return import_module(self.urlconf_name)
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/src/banzai_platform/urls.py", line 67, in <module>
url(r'^registrations/', include('registrations.urls')),
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/site-packages/django/conf/urls/__init__.py", line 50, in include
urlconf_module = import_module(urlconf_module)
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/src/registrations/urls.py", line 3, in <module>
from .views import (
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/src/registrations/views.py", line 160, in <module>
class ConfirmEventListView(ListView):
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/src/registrations/views.py", line 164, in ConfirmEventListView
queryset = [obj for obj in events if obj.need_confirmation > 0]
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/site-packages/django/db/models/query.py", line 250, in __iter__
self._fetch_all()
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/site-packages/django/db/models/query.py", line 1118, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/site-packages/django/db/models/query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 894, in execute_sql
raise original_exception
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 884, in execute_sql
cursor.execute(sql, params)
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/site-packages/django/db/backends/utils.py", line 80, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/Users/joedavy/Dropbox/Apps/banzai-platform-django/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "events_event" does not exist
LINE 1: ...ents_event"."created", "events_event"."slug" FROM "events_ev...
答:
2赞
Daniel Roseman
10/2/2017
#1
回溯显示,你正在 ConfirmEventListView 内的类级别对查询集运行列表推导式。首次导入类时,将执行类级别的任何内容;因此,此查询会立即执行任何 Django 命令,这是在迁移有机会运行之前。
绝不能执行任何访问此级别数据库的操作。(老实说,你根本不应该做这个列表推导,因为它返回的是列表而不是查询集;所以它不适用于分页或其他需要实际查询集的东西。但是,由于您的代码的其余部分可以正常工作,因此我们可以假设这在您的情况下还不是问题。
如果是字段,则查询集应定义为 。但是,如果它是模型方法,则应在 ConfirmEventListView 上定义一个方法,并在那里执行此处理,而不是在类级别执行此处理。need_confirmation
Event.objects.filter(need_confirmation__gt=0)
get_queryset()
评论
0赞
Joe Davy
10/2/2017
非常感谢 - 这解释了为什么我收到错误。那么类中的方法,例如没有计算? 是一个模型方法,用@property声明。关于如何返回查询集而不是基于模型方法的列表筛选的任何指导?get_queryset()
need_confirmation
1赞
Daniel Roseman
10/2/2017
区别在于,它是作为请求的一部分调用的,而不是在定义时调用的。事实上,正如刚才在另一个问题上向我指出的那样,从该方法返回列表是完全有效的;因此,只需将该列表 comp 移动到方法中并返回它,一切都应该很好。get_queryset
0赞
Joe Davy
10/2/2017
太棒了 - 做到了,现在似乎一切都在起作用。
评论