Django'Key(field)=()重复'

时间:2017-04-26 16:19:38

标签: python django postgresql wagtail

here之前已经问过这个问题,但是针对不同的情况。

我正在开发一个 Django / Wagtail 项目。在某些时候,我不得不修改模型,并添加一些字段。

无意中,我为新字段添加了一个标记 unique = True 。这样:

title = models.CharField(max_length=100, unique=True, blank=True, null=True, verbose_name=_('Category Title'))

在进行迁移和迁移时,它大吼大叫:

enter image description here

然后我意识到错误,删除 unique = True 并将其保留为:

title = models.CharField(max_length=100, blank=True, null=True, verbose_name=_('Category Title'))

我进行了迁移并再次迁移,期待这个问题消失。但是我得到了同样的问题。

我该如何解决这个问题?

这是最后一次迁移:

class Migration(migrations.Migration):

    dependencies = [
        ('distributed', '0029_remove_blogcategory_title'),
    ]

    operations = [
        migrations.AddField(
            model_name='blogcategory',
            name='title',
            field=models.CharField(blank=True, max_length=100, null=True, verbose_name='Category Title'),
        ),
    ]

这就是追踪:

    Microsoft Windows [Versión 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. Reservados todos los derechos.

C:\Windows\system32>cd C:/

C:\>cd distributed

C:\distributed>cd distributed

C:\distributed\distributed>python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have 6 unapplied migration(s). Your project may not work properly until you
apply the migrations for app(s): distributed.
Run 'python manage.py migrate' to apply them.
April 26, 2017 - 12:40:45
Django version 1.10.7, using settings 'genesis.settings.dev'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
^C
C:\distributed\distributed>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, distributed, otp_static, otp_
totp, otp_yubikey, sessions, sites, taggit, two_factor, wagtailadmin, wagtailcor
e, wagtaildocs, wagtailembeds, wagtailforms, wagtailimages, wagtailredirects, wa
gtailsearch, wagtailusers
Running migrations:
  Applying distributed.0025_auto_20170425_1827...Traceback (most recent call las
t):
  File "manage.py", line 11, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
367, in execute_from_command_line
    utility.execute()
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 294,
 in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 345,
 in execute
    output = self.handle(*args, **options)
  File "C:\Python27\lib\site-packages\django\core\management\commands\migrate.py
", line 204, in handle
    fake_initial=fake_initial,
  File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 11
5, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_i
nitial=fake_initial)
  File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 14
5, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_
initial)
  File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 24
4, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Python27\lib\site-packages\django\db\migrations\migration.py", line 1
29, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, projec
t_state)
  File "C:\Python27\lib\site-packages\django\db\migrations\operations\fields.py"
, line 84, in database_forwards
    field,
  File "C:\Python27\lib\site-packages\django\db\backends\postgresql\schema.py",
line 21, in add_field
    super(DatabaseSchemaEditor, self).add_field(model, field)
  File "C:\Python27\lib\site-packages\django\db\backends\base\schema.py", line 4
09, in add_field
    self.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\backends\base\schema.py", line 1
12, in execute
    cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 79, in
execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Python27\lib\site-packages\cachalot\monkey_patch.py", line 111, in in
ner
    out = original(cursor, sql, *args, **kwargs)
  File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 64, in
execute
    return self.cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 64, in
execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: could not create unique index "distributed_blogc
ategory_title_key"
DETAIL:  Key (title)=() is duplicated.


C:\distributed\distributed>

1 个答案:

答案 0 :(得分:2)

回溯告诉您尝试运行迁移0025时发生错误。

python manage.py showmigrations的输出告诉您尚未应用迁移0025-0030。

[X] 0022_auto_20170109_1620
[X] 0023_auto_20170109_1621
[X] 0024_auto_20170112_1933
[ ] 0025_auto_20170425_1827
[ ] 0026_auto_20170425_1829
[ ] 0027_auto_20170425_1839
[ ] 0028_auto_20170425_1840
[ ] 0029_remove_blogcategory_title
[ ] 0030_blogcategory_title

因此,删除这些迁移应该是安全的(您可能希望将它们备份为安全的一面)。然后,您可以重新运行makemigrationsmigrate以重新创建和运行新的迁移。

相关问题