[Python] Django migrate error

Discussão em 'Python' iniciado por Stack, Março 15, 2025.

  1. Stack

    Stack Membro Participativo

    I have been reading tango_with_django tutorial, and when I managed to get to part 7, I was stucked.

    When I added this code

    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


    I've got this

    Traceback (most recent call last): File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv) File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 385, in execute_from_command_line
    utility.execute() File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Python34\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__) File "C:\Python34\lib\site-packages\django\core\management\base.py", line 338, in execute
    output = self.handle(*args, **options) File "C:\Python34\lib\site-packages\django\core\management\commands\migrate.py ", line 161, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False)) File "C:\Python34\lib\site-packages\django\db\migrations\executor.py", line 68 , in migrate
    self.apply_migration(migration, fake=fake) File "C:\Python34\lib\site-packages\django\db\migrations\executor.py", line 10 2, in apply_migration
    migration.apply(project_state, schema_editor) File "C:\Python34\lib\site-packages\django\db\migrations\migration.py", line 1 08, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, ne w_state) File "C:\Python34\lib\site-packages\django\db\migrations\operations\fields.py" , line 37, in database_forwards
    field, File "C:\Python34\lib\site-packages\django\db\backends\sqlite3\schema.py", lin e 176, in add_field
    self._remake_table(model, create_fields=[field]) File "C:\Python34\lib\site-packages\django\db\backends\sqlite3\schema.py", lin e 144, in _remake_table
    self.quote_name(model._meta.db_table), File "C:\Python34\lib\site-packages\django\db\backends\schema.py", line 102, i n execute
    cursor.execute(sql, params) File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params) File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params) File "C:\Python34\lib\site-packages\django\db\utils.py", line 94, in
    __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback) File "C:\Python34\lib\site-packages\django\utils\six.py", line 658, in reraise

    raise value.with_traceback(tb) File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params) File "C:\Python34\lib\site-packages\django\db\backends\sqlite3\base.py", line 485, in execute
    return Database.Cursor.execute(self, query, params) django.db.utils.IntegrityError: UNIQUE constraint failed: rango_category__new.sl ug


    After I removed this code, I still get same error message while trying to do

    python manage.py migrate


    After I deleted database with

    python manage.py flush


    And entered

    python manage.py migrate


    I also got the same error.

    What's going on?

    Continue reading...

Compartilhe esta Página