我正在努力迁移数据库,而且我不确定为什么我会收到此错误。有谁知道如何解决这一问题?在此之前,我使用mysql创建了一个新数据库并授予用户访问权限。之前我有一个工作数据库但是应用程序没有工作(将OSX升级到El Capitan)所以我重新编写了它。我不确定我是否正确创建了新数据库。
> (env)DNab4046b2:VisualGenomeDev gmaister$ python manage.py migrate
> /Users/gmaister/Desktop/VisualGenomeDev/env/lib/python2.7/site-packages/django/test/_doctest.py:59:
> RemovedInDjango18Warning: The django.test._doctest module is
> deprecated; use the doctest module from the Python standard library
> instead. RemovedInDjango18Warning)
>
> /Users/gmaister/Desktop/VisualGenomeDev/env/lib/python2.7/site-packages/django/test/simple.py:27:
> RemovedInDjango18Warning: The django.test.simple module and
> DjangoTestSuiteRunner are deprecated; use
> django.test.runner.DiscoverRunner instead. RemovedInDjango18Warning)
>
> Operations to perform: Synchronize unmigrated apps: lockdown,
> django_extensions, corsheaders, rest_framework Apply all migrations:
> hits, sessions, V6W, djcelery, contenttypes, auth, sites, admin,
> DataAnalysis, RegionAnnotations, common, UserManagement, Sentences,
> Clusters, Evaluation, Canonicalization Synchronizing apps without
> migrations: Creating tables... Installing custom SQL...
> Installing indexes... Running migrations: Applying
> RegionAnnotations.0005_auto_20150716_1133...Traceback (most recent
> call last): File "manage.py", line 10, in <module>
> execute_from_command_line(sys.argv) File "/Users/gmaister/Desktop/VisualGenomeDev/env/lib/python2.7/site-packages/django/core/management/__init__.py",
> line 385, in execute_from_command_line
> utility.execute() File "/Users/gmaister/Desktop/VisualGenomeDev/env/lib/python2.7/site-packages/django/core/management/__init__.py",
> line 377, in execute
> self.fetch_command(subcommand).run_from_argv(self.argv) File "/Users/gmaister/Desktop/VisualGenomeDev/env/lib/python2.7/site-packages/django/core/management/base.py",
> line 288, in run_from_argv
> self.execute(*args, **options.__dict__) File "/Users/gmaister/Desktop/VisualGenomeDev/env/lib/python2.7/site-packages/django/core/management/base.py",
> line 338, in execute
> output = self.handle(*args, **options) File "/Users/gmaister/Desktop/VisualGenomeDev/env/lib/python2.7/site-packages/django/core/management/commands/migrate.py",
> line 160, in handle
> executor.migrate(targets, plan, fake=options.get("fake", False)) File
> "/Users/gmaister/Desktop/VisualGenomeDev/env/lib/python2.7/site-packages/django/db/migrations/executor.py",
> line 63, in migrate
> self.apply_migration(migration, fake=fake) File "/Users/gmaister/Desktop/VisualGenomeDev/env/lib/python2.7/site-packages/django/db/migrations/executor.py",
> line 97, in apply_migration
> migration.apply(project_state, schema_editor) File "/Users/gmaister/Desktop/VisualGenomeDev/env/lib/python2.7/site-packages/django/db/migrations/migration.py",
> line 107, in apply
> operation.database_forwards(self.app_label, schema_editor, project_state, new_state) File
> "/Users/gmaister/Desktop/VisualGenomeDev/env/lib/python2.7/site-packages/django/db/migrations/operations/fields.py",
> line 131, in database_forwards
> schema_editor.alter_field(from_model, from_field, to_field) File "/Users/gmaister/Desktop/VisualGenomeDev/env/lib/python2.7/site-packages/django/db/backends/schema.py",
> line 509, in alter_field
> self._alter_field(model, old_field, new_field, old_type, new_type, old_db_params, new_db_params, strict) File
> "/Users/gmaister/Desktop/VisualGenomeDev/env/lib/python2.7/site-packages/django/db/backends/schema.py",
> line 692, in _alter_field
> "extra": "", File "/Users/gmaister/Desktop/VisualGenomeDev/env/lib/python2.7/site-packages/django/db/backends/schema.py",
> line 98, in execute
> cursor.execute(sql, params) File "/Users/gmaister/Desktop/VisualGenomeDev/env/lib/python2.7/site-packages/django/db/backends/utils.py",
> line 81, in execute
> return super(CursorDebugWrapper, self).execute(sql, params) File "/Users/gmaister/Desktop/VisualGenomeDev/env/lib/python2.7/site-packages/django/db/backends/utils.py",
> line 65, in execute
> return self.cursor.execute(sql, params) File "/Users/gmaister/Desktop/VisualGenomeDev/env/lib/python2.7/site-packages/django/db/utils.py",
> line 94, in __exit__
> six.reraise(dj_exc_type, dj_exc_value, traceback) File "/Users/gmaister/Desktop/VisualGenomeDev/env/lib/python2.7/site-packages/django/db/backends/utils.py",
> line 65, in execute
> return self.cursor.execute(sql, params) File "/Users/gmaister/Desktop/VisualGenomeDev/env/lib/python2.7/site-packages/django/db/backends/mysql/base.py",
> line 128, in execute
> return self.cursor.execute(query, args) File "/Library/Python/2.7/site-packages/MySQLdb/cursors.py", line 205, in
> execute
> self.errorhandler(self, exc, value) File "/Library/Python/2.7/site-packages/MySQLdb/connections.py", line 36,
> in defaulterrorhandler
> raise errorclass, errorvalue django.db.utils.OperationalError: (1061, "Duplicate key name
> 'RegionAnnotations_regionannotation_phrase_ffbaf771d98f6cc_uniq'")
答案 0 :(得分:9)
我刚刚遇到这个错误,我可以通过使用这些SQL命令删除错误中指定的索引来解决它:
mysql> DROP INDEX [index_name] ON [table_name];
所以在你的情况下:
mysql> DROP INDEX RegionAnnotations_regionannotation_phrase_ffbaf771d98f6cc_uniq ON RegionAnnotations_regionannotation;
答案 1 :(得分:0)
您只需调整应用程序中的迁移文件,即可消除该列的唯一性。
例如我的问题:
# Generated by Django 2.2 on 2020-04-14 19:17
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('app_chapters', '0007_auto_20200414_2100'),
]
operations = [
migrations.AlterField(
model_name='chapter',
name='order',
field=models.IntegerField(unique=True),
),
migrations.AlterField(
model_name='chapter',
name='revelation_order',
field=models.IntegerField(),
),
]
更改为
# Generated by Django 2.2 on 2020-04-14 19:17
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('app_chapters', '0007_auto_20200414_2100'),
]
operations = [
migrations.AlterField(
model_name='chapter',
name='revelation_order',
field=models.IntegerField(),
),
]
另一种建议的解决方案是,如果可能的话,在phpMyAdmin或pgAdmin中修改SQL数据库。
答案 2 :(得分:-2)
如果此问题仅限于特定应用,您可以简单地伪造迁移(不是最佳做法):
python manage.py migrate [my_app_name] --fake
所以在你的情况下:
python manage.py migrate RegionAnnotations --fake