Django迁移错误

时间:2015-02-07 10:17:28

标签: python django django-models

我一直在阅读tango_with_django教程,当我设法进入第7部分时,我被困住了。

当我添加此代码时

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

我有这个

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

删除此代码后,尝试执行

时仍会收到相同的错误消息
python manage.py migrate

删除数据库后
python manage.py flush

并输入

python manage.py migrate

我也遇到了同样的错误。

发生了什么事?

1 个答案:

答案 0 :(得分:0)

尝试将迁移回滚到之前的状态。假设这是您的第一次迁移 - 让我们回滚到零

python manage.py migrate category zero
slu p逻辑可能会有点复杂 尽管名称不同,但他们可以拥有相同的slu .. 例如 姓名:Foo Bar Slug:foo-bar

姓名:foo吧 Slug:foo-bar

姓名:Foo Bar Slug:foo-bar

姓名:Foo!酒吧 Slug:foo-bar

......你明白了

你挑衅地想要独特的slu to使网址访问变得更好 例如domain.com/category/1234(使用id) vs domain.com/category/foo-bar

看一下AutoSlugField,它会为你做出很多努力,制作独特的slu ..