Answer the question
In order to leave comments, you need to log in
Django: IntegrityError: column user_id is not unique how to save correctly?
Hello, I have such a problem, when I ask a question, it doesn't answer, but this error pops up: "Django: IntegrityError: column user_id is not unique".
# models.py
class Question(models.Model):
title = models.CharField(max_length=120)
content = models.TextField()
pub_date = models.DateTimeField(blank=True, null=False, auto_now_add=True)
views = models.IntegerField(default=0)
slug = models.SlugField(max_length=240, blank=True)
tag = TaggableManager()
user = models.OneToOneField(User, unique=True)
def __unicode__(self):
return self.title
def save(self, **kwargs):
if not self.pk:
self.date_added = date.today()
if self.title and not self.slug:
self.slug = slugify(self.title)
super(Question, self).save(**kwargs)
def ask(request):
if request.method == 'POST':
form = AskForm(request.POST)
if form.is_valid():
question = form.save(commit=False)
question.user = request.user
question.save()
form.save_m2m()
return redirect('/question/one_question/' + str(question.id))
return HttpResponseBadRequest()
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/question/ask/
Django Version: 1.6.5
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'question',
'south',
'taggit',
'hitcount',
'registration',
'userprofile',
'user_profile',
'haystack')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware')
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
112. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/karmadorje/QandA/question/views.py" in ask
34. question.save()
File "/home/karmadorje/QandA/question/models.py" in save
33. super(Question, self).save(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save
545. force_update=force_update, update_fields=update_fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save_base
573. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in _save_table
654. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in _do_insert
687. using=using, raw=raw)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py" in _insert
232. return insert_query(self.model, objs, fields, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in insert_query
1514. return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py" in execute_sql
903. cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py" in execute
69. return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py" in execute
53. return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py" in __exit__
99. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py" in execute
53. return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py" in execute
451. return Database.Cursor.execute(self, query, params)
Exception Type: IntegrityError at /question/ask/
Exception Value: column user_id is not unique
Answer the question
In order to leave comments, you need to log in
Well, that's right, given the relationship between relationships. It turns out that a specific user can only ask one question, ForeignKey is more appropriate here. OneToOne is only relevant when one model "extends" another model.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question