• <i id='cqiK4'><tr id='cqiK4'><dt id='cqiK4'><q id='cqiK4'><span id='cqiK4'><b id='cqiK4'><form id='cqiK4'><ins id='cqiK4'></ins><ul id='cqiK4'></ul><sub id='cqiK4'></sub></form><legend id='cqiK4'></legend><bdo id='cqiK4'><pre id='cqiK4'><center id='cqiK4'></center></pre></bdo></b><th id='cqiK4'></th></span></q></dt></tr></i><div id='cqiK4'><tfoot id='cqiK4'></tfoot><dl id='cqiK4'><fieldset id='cqiK4'></fieldset></dl></div>

    1. <small id='cqiK4'></small><noframes id='cqiK4'>

    2. <legend id='cqiK4'><style id='cqiK4'><dir id='cqiK4'><q id='cqiK4'></q></dir></style></legend>

      • <bdo id='cqiK4'></bdo><ul id='cqiK4'></ul>
    3. <tfoot id='cqiK4'></tfoot>
      1. 在表“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上插入或更新在管理员中保存新模型时违反外键约束)
        1. <legend id='pOxnO'><style id='pOxnO'><dir id='pOxnO'><q id='pOxnO'></q></dir></style></legend>
        2. <small id='pOxnO'></small><noframes id='pOxnO'>

          <i id='pOxnO'><tr id='pOxnO'><dt id='pOxnO'><q id='pOxnO'><span id='pOxnO'><b id='pOxnO'><form id='pOxnO'><ins id='pOxnO'></ins><ul id='pOxnO'></ul><sub id='pOxnO'></sub></form><legend id='pOxnO'></legend><bdo id='pOxnO'><pre id='pOxnO'><center id='pOxnO'></center></pre></bdo></b><th id='pOxnO'></th></span></q></dt></tr></i><div id='pOxnO'><tfoot id='pOxnO'></tfoot><dl id='pOxnO'><fieldset id='pOxnO'></fieldset></dl></div>
            <tbody id='pOxnO'></tbody>

              • <bdo id='pOxnO'></bdo><ul id='pOxnO'></ul>
              • <tfoot id='pOxnO'></tfoot>

                  本文介绍了在表“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"上插入或更新在管理员中保存新模型时违反外键约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                  Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                  Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                  Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                  Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                  Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)
                    • <bdo id='o6yDX'></bdo><ul id='o6yDX'></ul>

                        <tfoot id='o6yDX'></tfoot>
                        <i id='o6yDX'><tr id='o6yDX'><dt id='o6yDX'><q id='o6yDX'><span id='o6yDX'><b id='o6yDX'><form id='o6yDX'><ins id='o6yDX'></ins><ul id='o6yDX'></ul><sub id='o6yDX'></sub></form><legend id='o6yDX'></legend><bdo id='o6yDX'><pre id='o6yDX'><center id='o6yDX'></center></pre></bdo></b><th id='o6yDX'></th></span></q></dt></tr></i><div id='o6yDX'><tfoot id='o6yDX'></tfoot><dl id='o6yDX'><fieldset id='o6yDX'></fieldset></dl></div>

                          <legend id='o6yDX'><style id='o6yDX'><dir id='o6yDX'><q id='o6yDX'></q></dir></style></legend>
                        • <small id='o6yDX'></small><noframes id='o6yDX'>

                              <tbody id='o6yDX'></tbody>