Django使迁移问题变为新的Postgres DB

时间:2018-11-14 16:47:30

标签: django postgresql django-migrations

我还无法找到适用于我的SO解决方案,所以我认为我会提出一个问题。

我遇到了使用django将数据库模式迁移到新的PostgreSQL DB的问题。

这是我第一个数据库的连接,它可以正常工作,并且我能够很好地运行迁移。这是我最初用于测试的数据库,现在我想使用辅助数据库并将架构迁移。

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'HOST': '[HOST]',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': '[PW]',
        }
    }

这是更新的设置:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'HOST': '[HOST]',
        'NAME': 'test_mh',       # this is only difference
        'USER': 'postgres',
        'PASSWORD': '[PW]',
        }
    }

我在第一个连接上运行 python3 manage.py makemigrations ,这很好。

当我使用更新后的数据库名称运行相同的命令时,出现异常,指出我的表不存在。

  Traceback (most recent call last):
    File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py", line 85, in _execute
      return self.cursor.execute(sql, params)
  psycopg2.ProgrammingError: relation "objects_community" does not exist
  LINE 1: ...y"."state", "objects_community"."date_added" FROM "objects_c...
                                                               ^


  The above exception was the direct cause of the following exception:

  Traceback (most recent call last):
    File "manage.py", line 15, in <module>
      execute_from_command_line(sys.argv)
    File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
      utility.execute()
    File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 375, in execute
      self.fetch_command(subcommand).run_from_argv(self.argv)
    File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 316, in run_from_argv
      self.execute(*args, **cmd_options)
    File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 350, in execute
      self.check()
    File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 379, in check
      include_deployment_checks=include_deployment_checks,
    File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 366, in _run_checks
      return checks.run_checks(**kwargs)
    File "/usr/local/lib/python3.6/dist-packages/django/core/checks/registry.py", line 71, in run_checks
      new_errors = check(app_configs=app_configs)
    File "/usr/local/lib/python3.6/dist-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique
      all_namespaces = _load_all_namespaces(resolver)
    File "/usr/local/lib/python3.6/dist-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces
      url_patterns = getattr(resolver, 'url_patterns', [])
    File "/usr/local/lib/python3.6/dist-packages/django/utils/functional.py", line 37, in __get__
      res = instance.__dict__[self.name] = self.func(instance)
    File "/usr/local/lib/python3.6/dist-packages/django/urls/resolvers.py", line 533, in url_patterns
      patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
    File "/usr/local/lib/python3.6/dist-packages/django/utils/functional.py", line 37, in __get__
      res = instance.__dict__[self.name] = self.func(instance)
    File "/usr/local/lib/python3.6/dist-packages/django/urls/resolvers.py", line 526, in urlconf_module
      return import_module(self.urlconf_name)
    File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
      return _bootstrap._gcd_import(name[level:], package, level)
    File "<frozen importlib._bootstrap>", line 994, in _gcd_import
    File "<frozen importlib._bootstrap>", line 971, in _find_and_load
    File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
    File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
    File "<frozen importlib._bootstrap_external>", line 678, in exec_module
    File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
    File "/home/viatech/Projects/ExactEstate/ExactEstate/ExactEstate/urls.py", line 21, in <module>
      path('adm/', include('interface_admin.urls')),
    File "/usr/local/lib/python3.6/dist-packages/django/urls/conf.py", line 34, in include
      urlconf_module = import_module(urlconf_module)
    File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
      return _bootstrap._gcd_import(name[level:], package, level)
    File "<frozen importlib._bootstrap>", line 994, in _gcd_import
    File "<frozen importlib._bootstrap>", line 971, in _find_and_load
    File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
    File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
    File "<frozen importlib._bootstrap_external>", line 678, in exec_module
    File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
    File "/home/viatech/Projects/ExactEstate/ExactEstate/interface_admin/urls.py", line 2, in <module>
      from interface_admin import views
    File "/home/viatech/Projects/ExactEstate/ExactEstate/interface_admin/views.py", line 4, in <module>
      from forms import RegistrationForm, SQLForm, SingleFileUploadForm, ApartmentTableFormBuilder
    File "/home/viatech/Projects/ExactEstate/ExactEstate/forms/ApartmentTableFormBuilder.py", line 10, in <module>
      class ApartmentTableFormBuilder(forms.Form):
    File "/home/viatech/Projects/ExactEstate/ExactEstate/forms/ApartmentTableFormBuilder.py", line 16, in ApartmentTableFormBuilder
      for community in Community.objects.all():
    File "/usr/local/lib/python3.6/dist-packages/django/db/models/query.py", line 268, in __iter__
      self._fetch_all()
    File "/usr/local/lib/python3.6/dist-packages/django/db/models/query.py", line 1186, in _fetch_all
      self._result_cache = list(self._iterable_class(self))
    File "/usr/local/lib/python3.6/dist-packages/django/db/models/query.py", line 54, in __iter__
      results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
    File "/usr/local/lib/python3.6/dist-packages/django/db/models/sql/compiler.py", line 1065, in execute_sql
      cursor.execute(sql, params)
    File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py", line 100, in execute
      return super().execute(sql, params)
    File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py", line 68, in execute
      return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
    File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
      return executor(sql, params, many, context)
    File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py", line 85, in _execute
      return self.cursor.execute(sql, params)
    File "/usr/local/lib/python3.6/dist-packages/django/db/utils.py", line 89, in __exit__
      raise dj_exc_value.with_traceback(traceback) from exc_value
    File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py", line 85, in _execute
      return self.cursor.execute(sql, params)
  django.db.utils.ProgrammingError: relation "objects_community" does not exist
  LINE 1: ...y"."state", "objects_community"."date_added" FROM "objects_c...

1 个答案:

答案 0 :(得分:2)

回溯告诉您,模块加载时,表单ApartmentTableFormBuilder导致查询Community.objects.all()

在新数据库上运行migratemakemigrations时,由于尚未创建objects_community表,因此会导致错误。

正确的解决方案是修改表单,以便在模块加载时不会引起任何查询。黑客解决方案是暂时注释掉引起问题的代码,直到您第一次运行makemigrationsmigrate