3

Hi m new in django framework and I am trying to solve this problem from 2 day I am getting this error after python manage.py migrate command. I cant understand what the problem is?

models.py

from django.db import models
from django.utils import timezone


class BlogPost(models.Model):
    author = models.ForeignKey('auth.User')
    title = models.CharField(max_length=100,unique=True)
    text = models.TextField()
    created_date = models.DateTimeField(default=timezone.now)
    published_date = models.DateTimeField(blank=True,null=True)

def publish(self):
    self.published_date=timezone.now()
    self.save()

def __str__(self):
    return self.title

error

  File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages   /django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 216, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
  File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 515, in alter_field
old_db_params, new_db_params, strict)
  File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages/django/db/backends/sqlite3/schema.py", line 262, in _alter_field
self._remake_table(model, alter_field=(old_field, new_field))
  File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages/django/db/backends/sqlite3/schema.py", line 206, in _remake_table
self.quote_name(model._meta.db_table),
  File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 120, in execute
cursor.execute(sql, params)
  File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages/django/db/backends/utils.py", line 80, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
  File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
  File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
  File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 328, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: UNIQUE constraint failed: social_blogpost.title

please help I am confused !!

5
  • 3
    The error is clearly straight to the point. You have defined title as unique and Django won't let you save same value twice for a unique field. Commented Jul 21, 2017 at 10:34
  • 1
    now what should i do now ? Commented Jul 21, 2017 at 11:06
  • 1
    What were you trying to do? If you don't want that unique constraint you can remove it Commented Jul 21, 2017 at 11:08
  • 1
    i want title should be unique no one can use it again , Is there any other method you know ? Commented Jul 21, 2017 at 11:13
  • You already have an object with the same title in the database. Delete it or edit it. Commented Jul 21, 2017 at 11:14

1 Answer 1

3

According to Django documentation, this is a summary of what the problem is;

Applying a “plain” migration that adds a unique non-nullable field to a table with existing rows will raise an error because the value used to populate existing rows is generated only once, thus breaking the unique constraint.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.