7

I'm working on migrating a database and I'm not sure why I'm getting this error. Does anyone know how to fix this? Before this, I created a new database using mysql and granted access to users. I had a working database before but the application wasn't working (upgrade OSX to El Capitan) so I redid it. I'm not sure I created the new database correctly.

> (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'")
2
  • 1
    You might have to delete your database and create a new one from scratch. There's some data that stuck in your old database and your migration would add some more data that violates the uniqueness. Commented Jan 4, 2016 at 19:16
  • I have the same error, if you read the error it tells you that you have a duplicate key name. It means that you are trying to create a new (unique) index in the database with a name that already exists. Don't know how to fix it yet... but deleting your database shouldn't fix the problem (but might be worth the try) Commented Jan 21, 2016 at 11:16

3 Answers 3

23

I just ran in to this error and I was able to solve it by dropping the index named in the error using these SQL commands:

mysql> DROP INDEX [index_name] ON [table_name];

so in your case:

mysql> DROP INDEX RegionAnnotations_regionannotation_phrase_ffbaf771d98f6cc_uniq ON RegionAnnotations_regionannotation;
Sign up to request clarification or add additional context in comments.

4 Comments

Don't know why this is down–voted. Works for me and solves my issue.
It happens when you have added db_index=True in previous migration. The above solution worked me.
MySQL said: #1553 - Cannot drop index '[index_name]': needed in a foreign key constraint
@user1309114 really helpful thanks. work fine for me
0

You can just adjust the migrations files in your app, removing the uniqueness of the column.

for example in my problem:

# 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(),
        ),
    ]

changed to

# 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(),
        ),
    ]

Another suggested solutions is to modify the SQL databases in phpMyAdmin or pgAdmin if possible.

Comments

-4

If this issue is only limited to a specific app, you can simply fake the migration (not the best practice) :

python manage.py migrate [my_app_name] --fake

so in your case:

python manage.py migrate RegionAnnotations --fake

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.