IntegrityError at /admin/api/user/6/change/ FOREIGN KEY constraint failed

IntegrityError at /admin/api/user/6/change/ FOREIGN KEY constraint failed

Problem Description:

I am developing a website on django. When I am trying to delete a user via admin panel i get an error. I can change e.g. staff status (while still getting an error, but changes are getting apllied) The code is below:

from django.contrib.auth.models import AbstractUser
from django.db import models

class User(AbstractUser):

    emailSpam = models.BooleanField(default=True)
    email = models.EmailField('email', unique=True)

    first_name = None
    last_name = None

    confirmedEmail = models.BooleanField(default=False)

    REQUIRED_FIELDS = ["emailSpam"]

from django.contrib.auth.forms import UserCreationForm, UserChangeForm

from .models import User

class CustomUserCreationForm(UserCreationForm):

    class Meta:
        model = User
        fields = ('email',)

class CustomUserChangeForm(UserChangeForm):

    class Meta:
        model = User
        fields = ('email',)

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin

from .forms import CustomUserCreationForm, CustomUserChangeForm
from .models import User

class Admin(UserAdmin):
    add_form = CustomUserCreationForm
    form = CustomUserChangeForm
    model = User
    list_display = ('email', 'is_staff', 'is_active',)
    list_filter = ('email', 'is_staff', 'is_active',)
    fieldsets = (
        (None, {'fields': ('email', 'password')}),
        ('Permissions', {'fields': ('is_staff', 'is_active')}),
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'password1', 'password2', 'is_staff', 'is_active')}
    search_fields = ('email',)
    ordering = ('email',), Admin)

Error message

Solution – 1

Possible solutions
There are three things that might be causing the issue, at least as far as I can tell. The first you already discounted. I hope it’s the second solution, since that will be easier, but I fear it might be the third, which would be hardest to get around.

Cause One
As my comment stated, perhaps there is another model with a field that has User as a ForeignKey with on_delete = models.CASCADE. When you try to delete the User, all instances of this class that has that ForeignKey will need to be deleted as well (because of on_delete=models.CASCADE), and that’s what’s causing the issue. You have already stated you have no such models, so let’s move on to solution 2.

Cause Two
I hope it’s this one, since it might be easier to fix. I noticed you have email = models.EmailField('email', unique=True) as one of your fields for your User model, but AbstractUser should already have an email field. Try removing that field, makemigrations and migrate, and see if the issue is resolved.

Cause Three
Did you change from the default User model to the custom user you are now using in mid-project? In other words, did you, in this project, ever run makemigrations before you switched to a custom user model? That can be a big problem. However, there are two solutions for this, not so easy or desirable, but doable.

Solution A: If this is a new project with no valuable data yet, you can simpy delete your database, delete all migrations in all folders, as well as all __pycache__ folders as described here. Then re do python makemigrations and python migrate. Of course this will erase all your tables, so you wouldn’t want to do this mid-project.

Solution B: There is a way to handle this mid-project without losing data. The steps are out in this django ticket 25313, or these, more readable instructions.

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.