I’m well into development of this project, I have a version in production and everything is working fine over there.
I recently discovered that if I try to create a new superuser, the user is not functional. The user is successfully created, it appears in my admin panel, everything appears to be normal, I can log in with the user, but when logged in as that user I attempt to modify another user through the admin panel, such as change their password or email, my app crashes with a FOREIGN KEY constraint failed.
The error happens when:
I create a new superuser (through the admin panel or through createsuperuser or through the shell, I did test all 3), log onto that new user and attempt to modify another user through the admin panel. FOREIGN KEY constraint failed happens as soon as I hit save and the user is not modified.
The error doesn’t happen when:
I use my original development superuser to do the same operations.
My custom user model:
class Employee(AbstractUser):
client = models.ForeignKey(Client,on_delete=models.CASCADE,blank=False,null=True,default=None)
ROLE_CHOICES = {
"OWNER": "Account owner (Head administrator)",
"ADMIN": "Administrator",
"USER": "User",
}
status = models.CharField(
choices=ROLE_CHOICES,
default="USER",
)
first_name = models.CharField(max_length=254,blank=False,default='Firstname')
last_name = models.CharField(max_length=254,blank=False,default='Lastname')
email = models.EmailField(max_length=254,blank=False,unique=True)
flha_in_use = models.ManyToManyField(FLHA,blank=True)
flha_templates = models.ManyToManyField(FLHATemplate,blank=True)
class Meta:
verbose_name = 'Employee'
verbose_name_plural = 'Employees'
def __str__(self):
return self.get_full_name()
The only ForeignKey is the custom model ‘Client’:
class Client(Group):
flha = models.ManyToManyField(FLHA,blank=True)
flha_templates = models.ManyToManyField(FLHATemplate,blank=True)
When I manually add a client through the shell, the error still happens.
When I add the client by modifying the user through the admin panel (using my original, functioning superuser), the client is updated successfully, I then log onto that user and the error still happens.
The error message:
|Django Version:|5.2.1|
| --- | --- |
|Exception Type:|IntegrityError|
|Exception Value:|FOREIGN KEY constraint failed|
|Exception Location:|C:\...\AppData\Local\Programs\Python\Python313\Lib\site-packages\django\db\backends\base\base.py, line 303, in _commit|
|Raised during:|django.contrib.admin.options.change_view|
|Python Executable:|C:\...\AppData\Local\Programs\Python\Python313\python.exe|
|Python Version:|3.13.3|
|Python Path:|```
['C:\\...\\Documents\\GitHub\\cs\\dev\\server',
'C:\\...\\AppData\\Local\\Programs\\Python\\Python313\\python313.zip',
'C:\\...\\AppData\\Local\\Programs\\Python\\Python313\\DLLs',
'C:\\...\\AppData\\Local\\Programs\\Python\\Python313\\Lib',
'C:\\...\\AppData\\Local\\Programs\\Python\\Python313',
'C:\\...\\AppData\\Local\\Programs\\Python\\Python313\\Lib\\site-packages']
```|
The traceback:
Environment:
Request Method: POST
Request URL: http://localhost:8000/admin/flowforms/employee/2/change/
Django Version: 5.2.1
Python Version: 3.13.3
Installed Applications:
['flowforms.apps.FlowformsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "C:\Users\Adminator\AppData\Local\Programs\Python\Python313\Lib\site-packages\django\db\backends\base\base.py", line 303, in _commit
return self.connection.commit()
^^^^^^^^^^^^^^^^^^^^^^^^
The above exception (FOREIGN KEY constraint failed) was the direct cause of the following exception:
File "C:\Users\Adminator\AppData\Local\Programs\Python\Python313\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Adminator\AppData\Local\Programs\Python\Python313\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Adminator\AppData\Local\Programs\Python\Python313\Lib\site-packages\django\contrib\admin\options.py", line 719, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Adminator\AppData\Local\Programs\Python\Python313\Lib\site-packages\django\utils\decorators.py", line 192, in _view_wrapper
result = _process_exception(request, e)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Adminator\AppData\Local\Programs\Python\Python313\Lib\site-packages\django\utils\decorators.py", line 190, in _view_wrapper
response = view_func(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Adminator\AppData\Local\Programs\Python\Python313\Lib\site-packages\django\views\decorators\cache.py", line 80, in _view_wrapper
response = view_func(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Adminator\AppData\Local\Programs\Python\Python313\Lib\site-packages\django\contrib\admin\sites.py", line 246, in inner
return view(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Adminator\AppData\Local\Programs\Python\Python313\Lib\site-packages\django\contrib\admin\options.py", line 1987, in change_view
return self.changeform_view(request, object_id, form_url, extra_context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Adminator\AppData\Local\Programs\Python\Python313\Lib\site-packages\django\utils\decorators.py", line 48, in _wrapper
return bound_method(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Adminator\AppData\Local\Programs\Python\Python313\Lib\site-packages\django\utils\decorators.py", line 192, in _view_wrapper
result = _process_exception(request, e)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Adminator\AppData\Local\Programs\Python\Python313\Lib\site-packages\django\utils\decorators.py", line 190, in _view_wrapper
response = view_func(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Adminator\AppData\Local\Programs\Python\Python313\Lib\site-packages\django\contrib\admin\options.py", line 1842, in changeform_view
with transaction.atomic(using=router.db_for_write(self.model)):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Adminator\AppData\Local\Programs\Python\Python313\Lib\site-packages\django\db\transaction.py", line 263, in __exit__
connection.commit()
^^^^^^^^^^^^^^^^^^^
File "C:\Users\Adminator\AppData\Local\Programs\Python\Python313\Lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Adminator\AppData\Local\Programs\Python\Python313\Lib\site-packages\django\db\backends\base\base.py", line 327, in commit
self._commit()
^^^^^^^^^^^^^^
File "C:\Users\Adminator\AppData\Local\Programs\Python\Python313\Lib\site-packages\django\db\backends\base\base.py", line 302, in _commit
with debug_transaction(self, "COMMIT"), self.wrap_database_errors:
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Adminator\AppData\Local\Programs\Python\Python313\Lib\site-packages\django\db\utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Adminator\AppData\Local\Programs\Python\Python313\Lib\site-packages\django\db\backends\base\base.py", line 303, in _commit
return self.connection.commit()
^^^^^^^^^^^^^^^^^^^^^^^^
Exception Type: IntegrityError at /admin/flowforms/employee/2/change/
Exception Value: FOREIGN KEY constraint failed
Does anyone have any clue as to what might be happening here?
Or potential solutions?
The inability to create a new superuser should something go wrong with the old one is truly a problem.