LoginSignup
5
2

More than 3 years have passed since last update.

【Django】管理サイトからデータベースにデータ登録時にIntegrityErrorが出たときの対処

Last updated at Posted at 2020-03-10

環境

  • Windows 10
  • Python 3.7.3
  • Django 2.2.2
  • PostgreSQL 10.12

背景

Djangoの入門書で勉強していたところ、管理サイトからデータベースにデータを作成するセクションで以下のようなエラーが出た。

IntegrityError at /admin/diary/diary/add/
������������"django_admin_log"������������������������������������������"django_admin_log_user_id_c564eba6_fk_auth_user_id"������������������������
DETAIL: ������������"auth_user"���������(user_id)=(3)������������������
Request Method: POST
Request URL: http://127.0.0.1:8000/admin/diary/diary/add/
Django Version: 2.2.2
Exception Type: IntegrityError
Exception Value:

������������"django_admin_log"������������������������������������������"django_admin_log_user_id_c564eba6_fk_auth_user_id"������������������������
DETAIL: ������������"auth_user"���������(user_id)=(3)������������������
Exception Location: C:\Users\grin\Anaconda3\envs\django\lib\site-packages\django\db\backends\base\base.py in _commit, line 240
Python Executable: C:\Users\grin\Anaconda3\envs\django\python.exe
Python Version: 3.7.3

django_admin_logの整合性エラーのようです。

django_admin_logテーブルに、古いauth_userテーブルとの外部キー関係が含まれていると発生するそうです。よって、これを削除してテーブルを再作成する必要があります。

対処法

$ python manage.py dbshell
DROP TABLE django_admin_log;
\q
$ python manage.py sqlmigrate admin 0001 | python manage.py dbshell

参考文献/URL

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2