EDIT1: I tried doing sqlflush to reset everything, but that didn't help.
EDIT2: I am able to create a user and specify OtherModel as NULL, and then edit the user later to make othermodel an actual reference. The problem occurs when I specify a reference during user creation.
So in my app, which is hooked up with postgres, I'm trying to extend the User model that django provides with a new model called UserProfile. I want each User to be associated with another model I created. So here's 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.
Everytime I try to make a new user, the user_id count keeps incrementing, which is weird because my actual user count stays 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?
The problem occurs because it appears you are trying to create a user profile and add the other model simultaneously in the admin.
Since the other model is a signal connected to the user creation, therefore, it is impossible.
Change the work flow such that you create the user, save user, create profile, save profile and add other model, save profile (in this order)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With