繁体   English   中英

Django 使迁移问题更改为新的 Postgres DB

Django make migrations issue changing to new Postgres DB

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文繁体   英文版本   中英对照 版本,有任何建议请联系yoyou2525@163.com。

我还没有找到对我有用的 SO 解决方案,所以我想我会发布一个问题。

我在使用 django 将数据库架构迁移到新的 PostgreSQL 数据库时遇到了问题。

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

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 个回复

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

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

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

2 将 postgres db 转换为迁移

我有一个我继承的数据库,并希望将数据库本身转换为可以在 ASP.NET Core 3 API 中使用的迁移。 如果可以,我希望不必手动编写迁移,而是将数据库转换为迁移。 我知道这对于 Entity Framework Core 来说非常容易,但由于性能原因以及个人偏好,我希望使用 dapper,而 ...

4 更改Django / Postgres db_table名称

我按照此处的说明进行操作: 在Django中更改表名称,以将django模型移动到其他应用程序。 它起作用了,但是现在我的模型正在引用旧表名 将该表的名称迁移回默认的countrys_currency的合适方法是什么(就像在country_currency应用程序中一样)? ...

5 Laravel Postgres多模式数据库迁移

我正在使用Postgres和Laravel 5.8。 我的数据库有多个架构。 每个模式都有一个patients表。 我正在通过在现有患者表中添加密码列来为每个架构中的患者创建身份验证。 迁移过程遍历每个架构,如果不存在,则应添加密码列。 在控制台和迁移表中成功创建了迁移,但在数据库表中未成功创建。 ...

6 克隆数据库的Django迁移

我克隆了一个postgres数据库,并向其中一个应用程序添加了新模型。 我们的项目包含许多应用程序。 现在,当我运行migrations , migrate将失败。 因此,我对该模型进行了注释,从文件夹中删除了migrations.py文件,并运行了假迁移。 然后再次将该模型放入并运 ...

2017-09-01 08:03:03 2 282   django
9 删除db.sqlite3 Django后无法进行迁移

在对模型进行了许多更改之后,我无法访问数据库。因此,我应用了以下解决方案: 删除所有迁移和db.sqlite3数据库,然后再次进行迁移。 但是,不幸的是,它不起作用。 我得到这样的错误: 如何再次进行迁移? ...

10 Django 2.0 + Postgres heroku迁移

我正在尝试在Heroku使用Postgres运行Django应用程序。 我已经为该应用程序创建了一个应用程序,但问题是该应用程序迁移未在Heroku中应用。 我正在使用heroku入门基础模板,并已提交了必要的迁移文件。 当我运行heroku run python manage.py ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2022 STACKOOM.COM