姜戈 'Key(field)=() is duplicated'
Django 'Key(field)=() is duplicated'
在 here 之前有人问过这个问题,但针对的是不同的场景。
我正在做一个 Django/Wagtail 项目。有时我不得不修改模型,并添加一些字段。
我不小心为新字段添加了一个标志 unique=True。这样:
title = models.CharField(max_length=100, unique=True, blank=True, null=True, verbose_name=_('Category Title'))
做迁移迁移的时候,大喊这个问题:
然后我意识到错误,删除了 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>
回溯告诉您当您尝试 运行 迁移 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
因此删除这些迁移应该是安全的(为了安全起见,您可能需要备份它们)。然后您可以重新运行 makemigrations
和 migrate
重新创建和 运行 新迁移。
在 here 之前有人问过这个问题,但针对的是不同的场景。
我正在做一个 Django/Wagtail 项目。有时我不得不修改模型,并添加一些字段。
我不小心为新字段添加了一个标志 unique=True。这样:
title = models.CharField(max_length=100, unique=True, blank=True, null=True, verbose_name=_('Category Title'))
做迁移迁移的时候,大喊这个问题:
然后我意识到错误,删除了 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>
回溯告诉您当您尝试 运行 迁移 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
因此删除这些迁移应该是安全的(为了安全起见,您可能需要备份它们)。然后您可以重新运行 makemigrations
和 migrate
重新创建和 运行 新迁移。