Still receiving save() prohited to prevent data loss due to unsaved related objec
gabn88 opened this issue · comments
While on allauth 61.1 we (sometimes) still receive the folllowing error:
Internal Server Error: /microsoft/login
ValueError at /microsoft/login
save() prohibited to prevent data loss due to unsaved related object 'app'.
Request Method: POST
Request URL: /microsoft/login
Django Version: 4.2.9
Python Executable: python3
Python Version: 3.11.2
Server time: Thu, 25 Apr 2024 10:08:50 +0000
Traceback (most recent call last):
File "python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "python3.11/site-packages/sentry_sdk/integrations/django/views.py", line 84, in sentry_wrapped_callback
return callback(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "python3.11/site-packages/django/views/decorators/csrf.py", line 56, in wrapper_view
return view_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "python3.11/site-packages/django/views/generic/base.py", line 104, in view
return self.dispatch(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "python3.11/site-packages/django/utils/decorators.py", line 46, in _wrapper
return bound_method(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/python3.11/site-packages/django/views/decorators/debug.py", line 92, in sensitive_post_parameters_wrapper
return view(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "python3.11/site-packages/dj_rest_auth/views.py", line 48, in dispatch
return super().dispatch(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "python3.11/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "python3.11/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "python3.11/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
^^^^^^^^^
File "python3.11/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "python3.11/site-packages/dj_rest_auth/views.py", line 125, in post
self.serializer.is_valid(raise_exception=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "python3.11/site-packages/rest_framework/serializers.py", line 223, in is_valid
self._validated_data = self.run_validation(self.initial_data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "python3.11/site-packages/rest_framework/serializers.py", line 445, in run_validation
value = self.validate(value)
^^^^^^^^^^^^^^^^^^^^
File backend/framework/api/v5/views.py", line 25, in validate
return super().validate(attrs)
^^^^^^^^^^^^^^^^^^^^^^^
File "python3.11/site-packages/dj_rest_auth/registration/serializers.py", line 161, in validate
ret = complete_social_login(request, login)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "python3.11/site-packages/allauth/socialaccount/helpers.py", line 220, in complete_social_login
return _complete_social_login(request, sociallogin)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "python3.11/site-packages/allauth/socialaccount/helpers.py", line 239, in _complete_social_login
ret = _process_signup(request, sociallogin)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "python3.11/site-packages/allauth/socialaccount/helpers.py", line 91, in _process_signup
get_adapter().save_user(request, sociallogin, form=None)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "python3.11/site-packages/allauth/socialaccount/adapter.py", line 102, in save_user
sociallogin.save(request)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "python3.11/site-packages/allauth/socialaccount/models.py", line 270, in save
self.token.save()
^^^^^^^^^^^^^^^^^
File "python3.11/site-packages/django/db/models/base.py", line 778, in save
self._prepare_related_fields_for_save(operation_name="save")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "python3.11/site-packages/django/db/models/base.py", line 1093, in _prepare_related_fields_for_save
raise ValueError(
^
Exception Type: ValueError at microsoft/login
Exception Value: save() prohibited to prevent data loss due to unsaved related object 'app'.
It looks like the save_user in the SocialAccountAdapter is the culprit, although I'm not 100% sure.
I don't think this is an issue in allauth. You're using dj-rest-auth, and that is assigning app
to token
without checking if app
is an instance backed by the database:
https://github.com/iMerica/dj-rest-auth/blob/master/dj_rest_auth/social_serializers.py#L68
Ah, sorry to bother you with it and thanks for pointing me to the right direction for a fix 👍