django - 完整性错误: null value in column "id" for all models/fields with ForeignKey after postgres restore from dump

标签 django postgresql heroku django-migrations django-2.0

我在尝试使用恢复本地 postgres 数据库的 heroku postgres 数据存储时遇到问题。使用恢复的 postgres 数据库 Django 正常运行。它检索所有对象并使用它们的字段、主键等,没有任何问题。

但是当涉及到写入数据库时​​,无论模型如何,我都会遇到同样的错误。

psycopg2.IntegrityError: null value in column "id" violates not-null constraint

当我重置heroku数据库并从空白状态创建对象时,没有任何问题。但是,如果我尝试在恢复的数据库上创建任何对象,我总是会得到这个列“id”中的空值违反了非空约束

<小时/>

这是尝试在 Django Admin 中创建基本模型时复制/粘贴的堆栈跟踪。我选择这个模型示例是因为没有与创建它相关的附加代码。没有信号或任何东西。

Django Version: 2.0 Python Version: 3.6.3

Traceback:

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py" in _execute 85. return self.cursor.execute(sql, params)

The above exception (null value in column "id" violates not-null constraint DETAIL: Failing row contains (null, Special Class, special-class). ) was the direct cause of the following exception:

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner 35. response = get_response(request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response 128. response = self.process_exception_by_middleware(e, request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response 126. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/options.py" in wrapper 574. return self.admin_site.admin_view(view)(*args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view 142. response = view_func(request, *args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func 44. response = view_func(request, *args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/sites.py" in inner 223. return view(request, *args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/options.py" in add_view 1553. return self.changeform_view(request, None, form_url, extra_context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper 62. return bound_func(*args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view 142. response = view_func(request, *args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/decorators.py" in bound_func 58. return func.get(self, type(self))(*args2, **kwargs2)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/options.py" in changeform_view 1450. return self._changeform_view(request, object_id, form_url, extra_context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/options.py" in _changeform_view 1490. self.save_model(request, new_object, form, not add)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/options.py" in save_model 1026. obj.save()

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py" in save 729. force_update=force_update, update_fields=update_fields)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py" in save_base 759. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py" in _save_table 842. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py" in _do_insert 880. using=using, raw=raw)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/manager.py" in manager_method 82. return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py" in _insert 1125. return query.get_compiler(using=using).execute_sql(return_id)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py" in execute_sql 1280. cursor.execute(sql, params)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py" in execute 100. return super().execute(sql, params)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py" in execute 68. return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py" in _execute_with_wrappers 77. return executor(sql, params, many, context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py" in _execute 85. return self.cursor.execute(sql, params)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py" in exit 89. raise dj_exc_value.with_traceback(traceback) from exc_value

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py" in _execute 85. return self.cursor.execute(sql, params)

Exception Type: IntegrityError at /admin/fantasy/raceclass/add/ Exception Value: null value in column "id" violates not-null constraint DETAIL: Failing row contains (null, Special Class, special-class).

<小时/>

来自堆栈跟踪的模型(请记住,每个模型都会发生此错误,而不仅仅是这个[非常基本的]模型。)

class RaceClass(models.Model):
    title = models.CharField(max_length=140)
    slug = models.SlugField(unique=True)

    def __str__(self):
        return self.title

    class Meta:
        ordering = ['title']
<小时/>

以下是我如何将本地数据恢复到heroku:

我正在使用命令转储本地 Postgres 数据库(版本 10.0):

PGPASSWORD=mypassword pg_dump -Fc --no-acl --no-owner -h localhost -U myuser mydb > mydb.dump

然后上传到 AWS,并使用命令恢复到 Heroku 上的 Postgres 数据存储(版本 9.6.5):

heroku pg:backups:restore 'https://s3.amazonaws.com/me/items/3H0q/mydb.dump' DATABASE_URL

这些都直接来自 Heroku 文档:https://devcenter.heroku.com/articles/heroku-postgres-import-export

<小时/>

旁注:我在本地使用 10.0 版 Postgres,Heroku 数据存储为 9.6.5

最佳答案

我相当确定这是因为您从 Postgres 10 导出并导入到 9。它并没有完全失败,但某些架构定义(在本例中为自动递增 ID 字段)不正确导入。

我可以想到两个选择:

  1. 尝试转储原始 SQL 而不是自定义格式:

    PGPASSWORD=mypassword pg_dump --no-acl --no-owner -h localhost -U myuser mydb > mydb.sql
    

    您无法使用 pg_restore 加载此内容 - 相反,您必须使用 psql 手动运行查询。像这样的东西应该有效:

    heroku pg:psql < mydb.sql
    

    这里需要注意的是,您首先需要清空现有数据库。

  2. 如果这也失败,那么您需要从要导入的相同主要版本的 Postgres 中导出。

关于django - 完整性错误: null value in column "id" for all models/fields with ForeignKey after postgres restore from dump,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47744721/

相关文章:

python - 如何解决类型错误: create_superuser() got an unexpected keyword argument 'paid' in django

sql - PostgreSQL 索引性能问题

Ruby 脚本卡在错误的 PG 连接调用上

postgresql - 在 PL/pgSQL 函数中使用变量

heroku - Heroku 上的 PWA - 将 HTTP 流量重定向到 HTTPS

ruby-on-rails - ActionMailer - 如何从 s3 添加附件

python - 将 Jupyter notebook 与从 Django 应用程序获取的数据集成

Django-获取当前的manage.py命令

python - Django:ImportError,没有名为 urls 的模块

python - ERROR : Command errored out with exit status 1: python setup. py Egg_info 检查日志以获取完整的命令输出。 [ flask ]