在表“django_admin_log"上插入或更新在管理员中保存新模型时违反外键约束

Insert or update on table quot;django_admin_logquot; violates foreign key constraint when saving new model in admin(在表“django_admin_log上插入或更新在管理员中保存新模型时违反外键约束)

本文介绍了在表“django_admin_log"上插入或更新在管理员中保存新模型时违反外键约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在管理员中保存模型的新实例时,我在回溯中收到以下 IntegrityError:

When saving a new instance of a model in the admin, I get the following IntegrityError in a traceback:

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".

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

The exact same code works perfectly on another machine.

推荐答案

看起来我的 Django 用户表不知何故变得陈旧并且包含一个过时的主键.不幸的是,运行 ./manage.py migrate 似乎并没有解决任何问题,但在 旧 Django trac 票证,我能够弄清楚如何轻松解决此问题(警告,您将丢失管理日志).

It looks like my Django user table somehow got stale and contained an outdated primary key. Unfortunately, running ./manage.py migrate didn't seem to fix anything, but with a little help from an old Django trac ticket, I was able to figure out how to easily fix this (warning, you will lose your admin logs).

./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 中运行更新以达到同样的效果,但我没有担心保留任何数据.

I'm sure I could run an update in PostgreSQL to the same effect, but I didn't have any data I was worried about keeping.

这篇关于在表“django_admin_log"上插入或更新在管理员中保存新模型时违反外键约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在表“django_admin_log"上插入或更新在管理员中保存新模型时违反外键约束

基础教程推荐