How to fix IntegrityError using the UserProfile model (when the user created the admin interface)?

EDIT1 : I tried doing sqlflush until reset everything, but that didn't help.

EDIT2 : I can create a user and specify OtherModel as NULL, and then edit the user later to make the other model a real link. The problem occurs when I provide a link during user creation.

So, in my postgres-related application, I'm trying to extend the User model that django provides with the new UserProfile model. I want each User to be associated with a different model that I created. So here is my code:

models.py

. . . class OtherModel(models.Model): # model info class UserProfile(models.Model): user = models.OneToOneField(User) othermodel = models.OneToOneField(OtherModel, null=True) def create_user_profile(sender, instance, created, **kwargs): if created: profile, created = UserProfile.objects.get_or_create(user=instance) post_save.connect(create_user_profile, sender=User) 

When I create a user from the django admin interface, I get the following error:

 IntegrityError at /admin/auth/user/add/ duplicate key value violates unique constraint "planamocal_userprofile_user_id_key" DETAIL: Key (user_id)=(23) already exists. 

Every time I try to create a new user, the user_id counter continues to increase, which is strange because my actual user count remains the same.

Here's the backtrace:

 Environment: Request Method: POST Request URL: http://127.0.0.1:8000/admin/auth/user/add/ Django Version: 1.3.1 Python Version: 2.7.1 Installed Applications: ['django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'planamocal', 'django.contrib.admin'] Installed Middleware: ('django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware') Traceback: File "/Users/AndyFang/Desktop/planamo/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 111. response = callback(request, *callback_args, **callback_kwargs) File "/Users/AndyFang/Desktop/planamo/venv/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper 307. return self.admin_site.admin_view(view)(*args, **kwargs) File "/Users/AndyFang/Desktop/planamo/venv/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view 93. response = view_func(request, *args, **kwargs) File "/Users/AndyFang/Desktop/planamo/venv/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func 79. response = view_func(request, *args, **kwargs) File "/Users/AndyFang/Desktop/planamo/venv/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner 197. return view(request, *args, **kwargs) File "/Users/AndyFang/Desktop/planamo/venv/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper 28. return bound_func(*args, **kwargs) File "/Users/AndyFang/Desktop/planamo/venv/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view 93. response = view_func(request, *args, **kwargs) File "/Users/AndyFang/Desktop/planamo/venv/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func 24. return func(self, *args2, **kwargs2) File "/Users/AndyFang/Desktop/planamo/venv/lib/python2.7/site-packages/django/db/transaction.py" in inner 217. res = func(*args, **kwargs) File "/Users/AndyFang/Desktop/planamo/venv/lib/python2.7/site-packages/django/contrib/auth/admin.py" in add_view 103. return super(UserAdmin, self).add_view(request, form_url, extra_context) File "/Users/AndyFang/Desktop/planamo/venv/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper 28. return bound_func(*args, **kwargs) File "/Users/AndyFang/Desktop/planamo/venv/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view 93. response = view_func(request, *args, **kwargs) File "/Users/AndyFang/Desktop/planamo/venv/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func 24. return func(self, *args2, **kwargs2) File "/Users/AndyFang/Desktop/planamo/venv/lib/python2.7/site-packages/django/db/transaction.py" in inner 217. res = func(*args, **kwargs) File "/Users/AndyFang/Desktop/planamo/venv/lib/python2.7/site-packages/django/contrib/admin/options.py" in add_view 885. self.save_formset(request, form, formset, change=False) File "/Users/AndyFang/Desktop/planamo/venv/lib/python2.7/site-packages/django/contrib/admin/options.py" in save_formset 677. formset.save() File "/Users/AndyFang/Desktop/planamo/venv/lib/python2.7/site-packages/django/forms/models.py" in save 482. return self.save_existing_objects(commit) + self.save_new_objects(commit) File "/Users/AndyFang/Desktop/planamo/venv/lib/python2.7/site-packages/django/forms/models.py" in save_new_objects 613. self.new_objects.append(self.save_new(form, commit=commit)) File "/Users/AndyFang/Desktop/planamo/venv/lib/python2.7/site-packages/django/forms/models.py" in save_new 717. obj.save() File "/Users/AndyFang/Desktop/planamo/venv/lib/python2.7/site-packages/django/db/models/base.py" in save 460. self.save_base(using=using, force_insert=force_insert, force_update=force_update) File "/Users/AndyFang/Desktop/planamo/venv/lib/python2.7/site-packages/django/db/models/base.py" in save_base 553. result = manager._insert(values, return_id=update_pk, using=using) File "/Users/AndyFang/Desktop/planamo/venv/lib/python2.7/site-packages/django/db/models/manager.py" in _insert 195. return insert_query(self.model, values, **kwargs) File "/Users/AndyFang/Desktop/planamo/venv/lib/python2.7/site-packages/django/db/models/query.py" in insert_query 1436. return query.get_compiler(using=using).execute_sql(return_id) File "/Users/AndyFang/Desktop/planamo/venv/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql 791. cursor = super(SQLInsertCompiler, self).execute_sql(None) File "/Users/AndyFang/Desktop/planamo/venv/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql 735. cursor.execute(sql, params) File "/Users/AndyFang/Desktop/planamo/venv/lib/python2.7/site-packages/django/db/backends/util.py" in execute 34. return self.cursor.execute(sql, params) File "/Users/AndyFang/Desktop/planamo/venv/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py" in execute 44. return self.cursor.execute(query, args) Exception Type: IntegrityError at /admin/auth/user/add/ Exception Value: duplicate key value violates unique constraint "planamocal_userprofile_user_id_key" DETAIL: Key (user_id)=(23) already exists. 

How to fix this error?

+6
source share
1 answer

The problem arises because it seems that you are trying to create a user profile and add another model at the same time in the admin.

Since the other model is a signal related to the creation of the user, therefore this is not possible.

Change the workflow so that you create the user, save the user, create the profile, save the profile and add another model, save the profile (in that order)

+6
source

Source: https://habr.com/ru/post/910562/


All Articles