# FOREIGN KEY constraint failed - Any newly created superuser causes the error when attempting to modify a user

**URL:** <https://forum.djangoproject.com/t/foreign-key-constraint-failed-any-newly-created-superuser-causes-the-error-when-attempting-to-modify-a-user/41053>\
**Category:** Mystery Errors\
**Created:** [May 22, 2025, 5:06pm UTC](https://forum.djangoproject.com/t/foreign-key-constraint-failed-any-newly-created-superuser-causes-the-error-when-attempting-to-modify-a-user/41053 "2025-05-22T17:06:41Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![cerealc0de](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/cerealc0de/32/28609_2.png) [@cerealc0de](https://forum.djangoproject.com/u/cerealc0de)\
**Post date:** [May 23, 2025, 4:21pm UTC](https://forum.djangoproject.com/t/foreign-key-constraint-failed-any-newly-created-superuser-causes-the-error-when-attempting-to-modify-a-user/41053/7 "2025-05-23T16:21:52Z")

</div>

I believe that’s the case.

I did find this other thread that seems to be the exact same issue as mine, where someone pointed out that running the first migration before changing AUTH\_USER\_MODEL can be the cause.

> [@IntegrityError: New admin user can't edit in the Dashboard](https://forum.djangoproject.com/t/integrityerror-new-admin-user-cant-edit-in-the-dashboard/31940):
>
> I am currently facing a very strange error and don’t know what to do. Summary As my first Django project I am currently programming a website for a betting game. I manage a large part of the data in the database with the Django admin dashboard for the sake of simplicity. Up to now I have always managed the data for tests with my first created superuser. Without any problems. Now I have deployed the website and created a second admin user. However, this user cannot make any changes/creations/de…

When I first built the database, I had to work around a certain issue, as a result I ran the first migration before changing AUTH\_USER\_MODEL. There’s likely an empty user table in the database, which would explain why it doesn’t matter how far back I pull my backup from, and would also explain why the production database is not having the issue (AUTH\_USER\_MODEL was configured before the first migration).

I did try to generate a superuser by manually creating specifically an Employee through the shell, constraint still failed. That part I found very strange, because both the target user and the admin user were created as an Employee object specifically. If the issue really is coming from the AUTH\_USER\_MODEL not being configured before the first migration, it sounds like it’s a django issue that should be addressed.

I’ll be investigating this potential cause a bit further and reporting back here.  
I’m probably just going to end up rebuilding the local database if I can’t find an answer very soon though, I do have deadlines and this issue has cost me 2 days of productivity.

---

_[View the full topic](https://forum.djangoproject.com/t/foreign-key-constraint-failed-any-newly-created-superuser-causes-the-error-when-attempting-to-modify-a-user/41053)._
