当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《在admin中保存新模型时,表"django_admin_log"上的插入或更新违反了外键约束》经验,为你挑选了1个好方法。

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

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



1> Dan Loewenhe..:

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

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

推荐阅读
手机用户2402852387
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有