在表格上插入或更新" django_admin_log"在admin中保存新模型时违反外键约束

时间:2015-12-05 18:27:54

标签: python django django-admin

在admin中保存模型的新实例时,我在追溯中得到以下IntegrityError:

Traceback:

File "/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/users/dan/project/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/users/dan/project/venv/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
  541.                 return self.admin_site.admin_view(view)(*args, **kwargs)

File "/users/dan/project/venv/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "/users/dan/project/venv/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)

File "/users/dan/project/venv/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
  244.             return view(request, *args, **kwargs)

File "/users/dan/project/venv/lib/python2.7/site-packages/django/contrib/admin/options.py" in add_view
  1435.         return self.changeform_view(request, None, form_url, extra_context)

File "/users/dan/project/venv/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
  67.             return bound_func(*args, **kwargs)

File "/users/dan/project/venv/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "/users/dan/project/venv/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
  63.                 return func.__get__(self, type(self))(*args2, **kwargs2)

File "/users/dan/project/venv/lib/python2.7/site-packages/django/utils/decorators.py" in inner
  184.                     return func(*args, **kwargs)

File "/users/dan/project/venv/lib/python2.7/site-packages/django/db/transaction.py" in __exit__
  223.                         connection.commit()

File "/users/dan/project/venv/lib/python2.7/site-packages/django/db/backends/base/base.py" in commit
  242.         self._commit()

File "/users/dan/project/venv/lib/python2.7/site-packages/django/db/backends/base/base.py" in _commit
  211.                 return self.connection.commit()

File "/users/dan/project/venv/lib/python2.7/site-packages/django/db/utils.py" in __exit__
  95.                 six.reraise(dj_exc_type, dj_exc_value, traceback)

File "/users/dan/project/venv/lib/python2.7/site-packages/django/db/backends/base/base.py" in _commit
  211.                 return self.connection.commit()

Exception Type: IntegrityError at /admin/app/invitation/add/
Exception Value: insert or update on table "django_admin_log" violates foreign key constraint "django_admin_log_user_id_52fdd58701c5f563_fk_auth_user_id"
DETAIL:  Key (user_id)=(3) is not present in table "auth_user".

完全相同的代码在另一台机器上完美运行。

4 个答案:

答案 0 :(得分:5)

看起来Django用户表以某种方式变得陈旧并包含过时的主键。不幸的是,运行./manage.py migrate似乎无法修复任何问题,但在old Django trac ticket的帮助下,我能够弄清楚如何轻松解决此问题(警告,您将丢失管理员日志)。

./manage.py migrate admin 0001
echo "DROP TABLE django_admin_log;" | ./manage.py dbshell
./manage.py sqlmigrate admin 0001 | python manage.py dbshell
./manage.py migrate admin

我确信我可以在PostgreSQL中运行更新以达到相同的效果,但我没有任何我担心保留的数据。

答案 1 :(得分:0)

您似乎以其他用户身份登录并更改了某些型号。因此,您需要从管理面板中注销,然后再次登录!

此解决方案对我来说效果很好。

答案 2 :(得分:0)

您可以注释掉已更新的模型。并使用以下命令运行迁移:

python manage.py makemigrations app.

现在取消注释模型,然后再次运行相同的makemigrations命令。所有详细信息也将从管理面板中删除。

答案 3 :(得分:-1)

删除数据库并重新创建它(Postgresql),它会解决问题。