Django.db.utils.IntegrityError: UNIQUE restriction failed: rango_category__new.slug

I learn Django from Tango using Django, but I get this error when I type:

python manage.py makemigrations rango python manage.py migrate 

This is the conclusion:

 django.db.utils.IntegrityError: UNIQUE constraint failed: rango_category__new.slug 

Models.py:

 from django.db import models from django.template.defaultfilters import slugify class Category(models.Model): name = models.CharField(max_length=128, unique=True) views = models.IntegerField(default=0) likes = models.IntegerField(default=0) slug = models.SlugField(unique=True) def save(self, *args, **kwargs): self.slug = slugify(self.name) super(Category, self).save(*args, **kwargs) def __unicode__(self): return self.name class Page(models.Model): category = models.ForeignKey(Category) title = models.CharField(max_length=128) url = models.URLField() views = models.IntegerField(default=0) def __unicode__(self): return self.title 
+11
source share
5 answers

The reason for this restriction may be that you did not have a field named slug in the Category class when you first transferred it (the first migration), and after adding this field to the model, when you started makemigrations , you set the default value for which Something of a static value (i.e. None or 'etc.) and which violated the unique delimiter for the column column of the Category table, in which slug must be unique, but this is not because the entire record will receive this default value.

To solve this problem, you can delete the database and migration files and re-run makemigrations and migrate or set a unique default value like this:

 slug = models.SlugField(unique=True, default=uuid.uuid1) 

Edit:

According to this , modify the migration file to overcome the unique delimiter. For example, modify the migration file (which added the bullet field to the model) as follows:

 import uuid from app.models import Category #where app == tango_app_name class Migration(migrations.Migration): dependencies = [ ('yourproject', '0003_remove_category_slug'), ] def gen_uuid(apps, schema_editor): for row in Category.objects.all(): row.slug = uuid.uuid4() row.save() operations = [ migrations.AddField( model_name='category', name='slug', field=models.SlugField(default=uuid.uuid4), preserve_default=True, ), migrations.RunPython(gen_uuid), migrations.AlterField( model_name='category', name='slug', field=models.SlugField(default=uuid.uuid4, unique=True), ), ] 
+15
source

I got a field with a unique attribute that was not unique [for example, 2 different values]

 python3 manage.py migrate --fake 

then

 python3 manage.py makemigrations python3 manage.py migrate 

it did the trick

+8
source

This means that the bullet must be unique. You may have some data in your model. You need to delete all data in this model, and you need to transfer again.

In this situation, you have two ways to fix the error;

  1. You must remove it from the Django admin site. Most often, this can lead to an error when trying to open the model.

  2. Open command line

 move to project -> py manage.py shell -> from yourappname.models import modelname -> modelname.objects.delete() 

Here, if you define a product manager for your model. Then you must define the delete function. Later you should makemigrate , migrate and continue the second way

+1
source

It worked for me that I contacted the administrator and changed the value of the duplicate slug before starting the migration again.

0
source
 python manage.py flush 

Did the trick. Cruel but effective. Warning! All your data in the database will be lost. You will need to create a superuser and so on ...

-5
source

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


All Articles