Django迁移失败了新的数据库

时间:2017-10-01 19:04:06

标签: django postgresql django-south django-migrations

想知道是否有其他人在使用Django迁移时遇到令人沮丧的行为。我已经查看了SO上的几个相关帖子,但没有找到任何解决这个问题的具体内容。

这种情况一直在发生,它必须是一种常见的情况,所以我错过了什么?

我的情景:

  1. 列表项
  2. 我已经构建了一个Django应用程序,在本地计算机上使用sqlite进行迭代
  3. 我现在想要部署到生产Postgresql数据库(AWS或其他地方)
  4. 我设置了新数据库,将其添加到我的设置中,然后运行python manage.py migrate
  5. 我收到一条SQL错误,告诉我我的某个模型表不存在。咄!这就是我正在运行迁移的原因。
  6. 我最终不得不手工制作表格,做一些奇怪的事情,例如python manage.py migrate <app_name> --fake,这会产生包括simple_history
  7. 等软件包的进一步问题

    我确信必须有一个明显的答案 - 我应该怎么做才能在一个全新的,干净的数据库上设置数据库架构?

    编辑:我收到错误的追溯:

    当我在空白数据库上运行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...
    

1 个答案:

答案 0 :(得分:1)

您的回溯显示您正在ConfirmEventListView内的类级别的查询集上运行列表解析。在类首次导入时执行类级别的任何操作;因此,在执行任何Django命令之前立即执行此查询,这是在迁移有机会运行之前。

您绝不能在此级别执行任何访问数据库的操作。 (说实话,你根本不应该做这个列表理解,因为它返回的是一个列表而不是一个查询集;所以它不适用于分页或其他期望实际查询集的东西。但是,因为你的其余部分代码有效,我们可以假设这不是你的问题 - 但是。)

如果need_confirmation是字段,则您的查询集应该只定义为Event.objects.filter(need_confirmation__gt=0)。但是,如果它是一个模型方法,那么你应该在ConfirmEventListView上定义一个get_queryset()方法并在那里进行处理,而不是在类级别。