我在 django 中删除用户时遇到 IntegrityError

问题描述

当我删除 user 时,出现此错误

IntegrityError at /api/user/25/
update or delete on table "profiles_customuser" violates foreign key constraint "registration_registr_user_id_5fcbf725_fk_profiles_" on table "registration_registrationprofile"
DETAIL:  Key (id)=(25) is still referenced from table "registration_registrationprofile".


Request Method: DELETE
Request URL: https://dev.onepaper.biz/api/user/25/

 File "/home/ubuntu/djangovenv/lib/python3.8/site-packages/django/db/backends/base/base.py",line 242,in _commit
   return self.connection.commit()

The above exception (update or delete on table "profiles_customuser" violates foreign key constraint "registration_registr_user_id_5fcbf725_fk_profiles_" on table "registration_registrationprofile"
DETAIL:  Key (id)=(25) is still referenced from table "registration_registrationprofile".
) was the direct cause of the following exception:
 File "/home/ubuntu/djangovenv/lib/python3.8/site-packages/django/core/handlers/exception.py",line 47,in inner
   response = get_response(request)
 File "/home/ubuntu/djangovenv/lib/python3.8/site-packages/django/core/handlers/base.py",line 181,in _get_response
   response = wrapped_callback(request,*callback_args,**callback_kwargs)
 File "/home/ubuntu/djangovenv/lib/python3.8/site-packages/django/views/decorators/csrf.py",line 54,in wrapped_view
   return view_func(*args,**kwargs)
 File "/home/ubuntu/djangovenv/lib/python3.8/site-packages/rest_framework/viewsets.py",line 125,in view
   return self.dispatch(request,*args,**kwargs)
 File "/home/ubuntu/djangovenv/lib/python3.8/site-packages/rest_framework/views.py",line 509,in dispatch
   response = self.handle_exception(exc)
 File "/home/ubuntu/djangovenv/lib/python3.8/site-packages/rest_framework/views.py",line 469,in handle_exception
   self.raise_uncaught_exception(exc)
 File "/home/ubuntu/djangovenv/lib/python3.8/site-packages/rest_framework/views.py",line 480,in raise_uncaught_exception
   raise exc
 File "/home/ubuntu/djangovenv/lib/python3.8/site-packages/rest_framework/views.py",line 506,in dispatch
   response = handler(request,**kwargs)
 File "/home/ubuntu/onepaper-green/profiles/views.py",line 320,in destroy
   instance.delete()
 File "/home/ubuntu/djangovenv/lib/python3.8/site-packages/django/db/models/base.py",line 947,in delete
   return collector.delete()
 File "/home/ubuntu/djangovenv/lib/python3.8/site-packages/django/db/models/deletion.py",line 435,in delete
   signals.post_delete.send(
 File "/home/ubuntu/djangovenv/lib/python3.8/site-packages/django/db/transaction.py",line 232,in __exit__
   connection.commit()
 File "/home/ubuntu/djangovenv/lib/python3.8/site-packages/django/utils/asyncio.py",line 26,in inner
   return func(*args,**kwargs)
 File "/home/ubuntu/djangovenv/lib/python3.8/site-packages/django/db/backends/base/base.py",line 266,in commit
   self._commit()
 File "/home/ubuntu/djangovenv/lib/python3.8/site-packages/django/db/backends/base/base.py",in _commit
   return self.connection.commit()
 File "/home/ubuntu/djangovenv/lib/python3.8/site-packages/django/db/utils.py",line 90,in __exit__
   raise dj_exc_value.with_traceback(traceback) from exc_value
 File "/home/ubuntu/djangovenv/lib/python3.8/site-packages/django/db/backends/base/base.py",in _commit
   return self.connection.commit()

Exception Type: IntegrityError at /api/user/25/
Exception Value: update or delete on table "profiles_customuser" violates foreign key constraint "registration_registr_user_id_5fcbf725_fk_profiles_" on table "registration_registrationprofile"
DETAIL:  Key (id)=(25) is still referenced from table "registration_registrationprofile".

将 django-registration-redux(django-registration) 移至 django-allauth 存在一些问题。 所以我试图删除 registration_registrationprofile 表。 但我找不到那张桌子。

我只能看到那些表。 它看起来与这个问题无关。 如何成功删除用户

account_emailaddress                  papers_paperstatus
account_emailconfirmation             papers_signature
addresses_address                     papers_verifyingexplanation
auth_group                            profiles_alloweduser
auth_group_permissions                profiles_alloweduser_allowed_users
auth_permission                       profiles_customuser
authtoken_token                       profiles_customuser_groups
django_admin_log                      profiles_customuser_user_permissions
django_content_type                   profiles_expertprofile
django_migrations                     profiles_mandate
django_session                        profiles_profile
django_site                           socialaccount_socialaccount
papers_contractor                     socialaccount_socialapp
papers_explanationsignature           socialaccount_socialapp_sites
papers_paper                          socialaccount_socialtoken

我猜当我使用 django-registration-redux 时它有一些额外的 FK 信息但是在移动到 django-allauth 之后,它不再需要了。 怎样才能成功删除

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)