IntegrityError: null value in column "id" for all models/fields with ForeignKey after postgres restore from dump
IntegrityError: null value in column "id" for all models/fields with ForeignKey after postgres restore from dump
我 运行 遇到问题,试图从我拥有的本地 postgres 数据库的恢复中使用 heroku postgres 数据存储。使用恢复后的 postgres 数据库 Django 正常运行。它检索所有对象并使用它们的字段、主键等,没有任何问题。
但是当涉及到写入数据库时,无论型号如何,我都会遇到同样的错误。
psycopg2.IntegrityError: null value in column "id" violates not-null constraint
当我重置 heroku 数据库并从空白板创建对象时,没有任何问题。但是如果我尝试在恢复的数据库上创建任何对象,我总是得到这个 null value in column "id" violates not-null constraint
这里是 copy/pasted 尝试在 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']
以下是我将本地数据恢复(d)到 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 Datastore 是 9.6.5
我相当确定这是因为您正在从 Postgres 10 导出并导入到 9。它并没有完全失败,但是一些模式定义(在本例中是自动递增的 ID 字段)没有正在正确导入。
我能想到两个方案:
尝试转储原始 SQL 而不是自定义格式:
PGPASSWORD=mypassword pg_dump --no-acl --no-owner -h localhost -U myuser mydb > mydb.sql
您不能使用 pg_restore
加载它 - 您必须使用 psql
手动 运行 查询。这样的事情应该有效:
heroku pg:psql < mydb.sql
这里需要注意的是,您首先需要清空现有数据库。
如果这也失败,那么您需要从您要导入到的相同主版本的 Postgres 中导出。
关键错误是这样的:
I'm using Version 10.0 Postgres locally and Heroku Datastore is 9.6.5
这是一个等待发生的问题。我会尝试在两者上使用相同的版本。至少相同的主要版本。
特别想到这两个的是标准-SQL IDENTITY
columns in Postgres 10 的引入,这主要是为了取代串行列。你没有透露 table 定义,所以我只能猜测。 Postgres 10 中的 IDENTITY
功能不会转换回 Postgres 9.6,这可以很好地解释错误消息中违反 NULL 值的原因。
相关:
- Auto increment table column
我最近遇到了同样的问题,以下是对我有用的方法:
- 创建新数据库
使用 Django manage.py 转储我的数据库的数据并在应用所有迁移后将其恢复到新数据库。
python manage.py dumpdata --exclude auth.permission --exclude contenttypes --indent 2 > db.json
恢复备份
pythonmanage.py加载数据db.json
我 运行 遇到问题,试图从我拥有的本地 postgres 数据库的恢复中使用 heroku postgres 数据存储。使用恢复后的 postgres 数据库 Django 正常运行。它检索所有对象并使用它们的字段、主键等,没有任何问题。
但是当涉及到写入数据库时,无论型号如何,我都会遇到同样的错误。
psycopg2.IntegrityError: null value in column "id" violates not-null constraint
当我重置 heroku 数据库并从空白板创建对象时,没有任何问题。但是如果我尝试在恢复的数据库上创建任何对象,我总是得到这个 null value in column "id" violates not-null constraint
这里是 copy/pasted 尝试在 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']
以下是我将本地数据恢复(d)到 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 Datastore 是 9.6.5
我相当确定这是因为您正在从 Postgres 10 导出并导入到 9。它并没有完全失败,但是一些模式定义(在本例中是自动递增的 ID 字段)没有正在正确导入。
我能想到两个方案:
尝试转储原始 SQL 而不是自定义格式:
PGPASSWORD=mypassword pg_dump --no-acl --no-owner -h localhost -U myuser mydb > mydb.sql
您不能使用
pg_restore
加载它 - 您必须使用psql
手动 运行 查询。这样的事情应该有效:heroku pg:psql < mydb.sql
这里需要注意的是,您首先需要清空现有数据库。
如果这也失败,那么您需要从您要导入到的相同主版本的 Postgres 中导出。
关键错误是这样的:
I'm using Version 10.0 Postgres locally and Heroku Datastore is 9.6.5
这是一个等待发生的问题。我会尝试在两者上使用相同的版本。至少相同的主要版本。
特别想到这两个的是标准-SQL IDENTITY
columns in Postgres 10 的引入,这主要是为了取代串行列。你没有透露 table 定义,所以我只能猜测。 Postgres 10 中的 IDENTITY
功能不会转换回 Postgres 9.6,这可以很好地解释错误消息中违反 NULL 值的原因。
相关:
- Auto increment table column
我最近遇到了同样的问题,以下是对我有用的方法:
- 创建新数据库
使用 Django manage.py 转储我的数据库的数据并在应用所有迁移后将其恢复到新数据库。
python manage.py dumpdata --exclude auth.permission --exclude contenttypes --indent 2 > db.json
恢复备份
pythonmanage.py加载数据db.json