'Django Token objects update error : django.db.utils.IntegrityError: UNIQUE constraint failed: authtoken_token.user_id

I was using django user class and rest_framework Token class to store the user info and the token. For the same I was using serializers.ModelSerializer class. But when I am making update request(check update method) to update the user info as well as the token that I have, its giving me error.

Here is serializers.py

from rest_framework import serializers
from django.contrib.auth.models import User
from rest_framework.authtoken.views import Token

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['id', 'username', 'password']

        extra_kwargs = {
            'password' : {
                'write_only':True,
                'required': True
            }
        }
    
    def create(self, validated_data):
        user = User.objects.create_user(**validated_data)
        Token.objects.create(user=user) # create token for the user
        return user

    def update(self, instance, validated_data):
        instance.username = validated_data['username']
        instance.set_password(validated_data['password'])
        instance.save()
        Token.objects.update(user=instance)

        return instance

views.py

class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer

    permission_classes = [IsAuthenticated, IsOwnerOfObject]
    authentication_classes = (TokenAuthentication,)

urls.py

from django.urls import path, include
from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register('users', UserViewSet, basename = 'users')

urlpatterns = [
    path('api/', include(router.urls)),
]

Error :

django.db.utils.IntegrityError: UNIQUE constraint failed: authtoken_token.user_id

This is how I am making the request with the authorisation token in the header field:

enter image description here



Solution 1:[1]

What do you want to achieve?

Token.objects.update(user=instance)

It doesn't make any sense. It is update without WHERE clause.

I think that you "want" to do something like this.

Token.objects.filter(user=instance).update(user=instance)

That query does nothing really.

I think that you really want is token invalidation. You must delete the current one and generate new.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marc_s