Foreign Key Constraint Failed in Django Admin Panel When Creating/Editing/Deleting a User. (Using Custom User Model.)

FOREIGN KEY constraint failed in django admin panel when creating/editing/deleting a user. (Using custom user model.)

I asked around and the code should work in older versions of django. Sadly it won't work in django 2.0 or above. If anyone wants an alternative I found this to be perfect for my project, explanation is simple as well.

Unable to Create/Edit/Delete objects on Django Admin - Foreign Key Constraint Failed

https://code.djangoproject.com/ticket/23297

This solved my issue. It seems the foreign key constraint was resulting from a reference to the previous user model, before migrating to my custom user model midway through development.

How to fix IntegrityError: FOREIGN KEY constraint failed

It's because you've created super user before writing this code I guess. So your admin user haven't linked with any profile yet.

You can do this in three ways:

  1. Recreate your superuser by python3 manage.py createsuperuser

  2. Update your save method

     @receiver(post_save, sender=CustomUser)
    def save_user_profile(sender, instance, **kwargs):
    try:
    instance.userprofile.save()
    except ObjectDoesNotExist:
    UserProfile.objects.create(user=instance)
  3. Drop db and migration files and migrate with new db.

How to resolve FOREIGN KEY constraint failed

I think i have found a solution for this. The problem could well be caused by the circular dependencies issues when you migrate your default AUTH_USER_MODEL to a custom model in the middle of the project.

From Django Documentation

Changing AUTH_USER_MODEL after you’ve created database tables is significantly more difficult since it affects foreign keys and many-to-many relationships, for example.

This change can’t be done automatically and requires manually fixing your schema, moving your data from the old user table, and possibly manually reapplying some migrations. See #25313 for an outline of the steps.

Due to limitations of Django’s dynamic dependency feature for swappable models, the model referenced by AUTH_USER_MODEL must be created in the first migration of its app (usually called 0001_initial); otherwise, you’ll have dependency issues.

In addition, you may run into a CircularDependencyError when running your migrations as Django won’t be able to automatically break the dependency loop due to the dynamic dependency. If you see this error, you should break the loop by moving the models depended on by your user model into a second migration. (You can try making two normal models that have a ForeignKey to each other and seeing how makemigrations resolves that circular dependency if you want to see how it’s usually done.)

The best way to tackle this is to drop the table and remove all migration files and then re run the migrations with your newly made custom model. Hope this will work.

More details on how to migrate from a built in model to a new model can be found here
https://code.djangoproject.com/ticket/25313



Related Topics



Leave a reply



Submit