私は持っているローカルpostgresデータベースの復元からheroku postgresデータストアを使用しようとする問題に遭遇しています。復元されたpostgresデータベースを使用すると、Djangoは正常に動作します。すべてのオブジェクトを取得し、フィールド、primayキーなどを問題なく使用します。IntegrityError:ダンプからポストグルを復元した後、ForeignKeyを持つすべてのモデル/フィールドのカラム "id"のヌル値
しかし、データベースに書き込む際には、モデルに関係なく、全面的に同じエラーが発生します。
psycopg2.IntegrityError: null value in column "id" violates not-null constraint
私はherokuデータベースをリセットし、空白のスレートからオブジェクトを作成するときに問題はありません。私は復元されたデータベース上の任意のオブジェクトを作成しようとした場合でも、私はいつもジャンゴ管理における基本的なモデルを作成しようとしているから、このnull value in column "id" violates not-null constraint
ここだコピー/貼り付けスタックトレースを取得します。このモデルの例は、その作成に関連する追加のコードがないために選んだものです。シグナルなどはありません。
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).
スタックトレースからモデル(1 [非常に基本的な]だけでなく、この、このエラーはすべてのモデルに起こる心に留めておいてください。)
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']
ここで方法です私は英雄にローカルデータを復元します:
私はローカルのPostgresデータベースをダンプしています(V Herokuの使用コマンドに
そしてPGPASSWORD=mypassword pg_dump -Fc --no-acl --no-owner -h localhost -U myuser mydb > mydb.dump
AWSへのアップロード、およびPostgresのデータストアにリストア(バージョン9.6.5):ERSION 10.0)を使用してコマンド
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
です
あなたはエラーの完全なスタックトレース、およびエラーが発生したとき(あなたが保存しようとしているものは何でもモデルのために)実行するコードを投稿することができますしてください、および関連するモデルクラス。これを見ると、問題はデータベースの復元ではなく、データベースに書き込もうとしているように見えます。 – solarissmoke
@solarissmokeスタックトレースとモデルコードを追加しました。私が見つけられる最も基本的なものは、すべてが問題になってきたものです。あなたが助けることを願っています! – taylor