Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django database migration error: duplicate key

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'")
like image 970
gijoe Avatar asked Jan 04 '16 19:01

gijoe


1 Answers

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;
like image 186
user1309114 Avatar answered Nov 19 '22 04:11

user1309114