EDIT1 :我尝试使用sqlflush重置所有内容,但这没有帮助。
EDIT2 :我可以创建用户并将OtherModel指定为NULL,然后稍后编辑用户以使othermodel成为实际引用。在用户创建期间指定引用时会发生此问题。
所以在我的应用程序中,它与postgres连接,我正在尝试扩展django提供的User模型,其中包含一个名为UserProfile的新模型。我希望每个用户都与我创建的另一个模型相关联。所以这是我的代码:
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)
当我从django管理界面创建用户时,出现以下错误:
IntegrityError at /admin/auth/user/add/
duplicate key value violates unique constraint "planamocal_userprofile_user_id_key"
DETAIL: Key (user_id)=(23) already exists.
每次我尝试创建一个新用户时,user_id计数都会递增,这很奇怪,因为我的实际用户数保持不变。
这是回溯:
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.
如何解决此错误?
答案 0 :(得分:7)
出现此问题是因为您似乎正在尝试创建用户配置文件并在管理中同时添加其他模型。
因为其他模型是连接到用户创建的信号,所以不可能。
更改工作流程,以便您创建用户,保存用户,创建配置文件,保存配置文件并添加其他模型,保存配置文件(按此顺序)