#22844: SQLite3 migrations fail with unique_together on a ForeignKey
-------------------------------+--------------------------------
     Reporter:  fongandrew     |      Owner:  nobody
         Type:  Uncategorized  |     Status:  new
    Component:  Uncategorized  |    Version:  1.7-beta-2
     Severity:  Normal         |   Keywords:  migrations, sqlite
 Triage Stage:  Unreviewed     |  Has patch:  0
Easy pickings:  0              |      UI/UX:  0
-------------------------------+--------------------------------
 This models.py:

 {{{
 from django.db import models
 from django.contrib.auth.models import User

 class DropboxAccount(models.Model):
     """
     A Dropbox account linked to an existing user account
     """
     user = models.ForeignKey(User)
     dropbox_uid = models.CharField(max_length=32)
     class Meta:
         unique_together = (
             ("user", "dropbox_uid"),
         )
 }}}

 creates the following exception during migration:

 {{{
 Creating test database for alias 'default'...
 Traceback (most recent call last):
   File "manage.py", line 10, in <module>
     execute_from_command_line(sys.argv)
   File
 "/home/vagrant/deployment/src/django/django/core/management/__init__.py",
 line 385, in execute_from_command_line
     utility.execute()
   File
 "/home/vagrant/deployment/src/django/django/core/management/__init__.py",
 line 377, in execute
     self.fetch_command(subcommand).run_from_argv(self.argv)
   File
 "/home/vagrant/deployment/src/django/django/core/management/commands/test.py",
 line 50, in run_from_argv
     super(Command, self).run_from_argv(argv)
   File
 "/home/vagrant/deployment/src/django/django/core/management/base.py", line
 288, in run_from_argv
     self.execute(*args, **options.__dict__)
   File
 "/home/vagrant/deployment/src/django/django/core/management/commands/test.py",
 line 71, in execute
     super(Command, self).execute(*args, **options)
   File
 "/home/vagrant/deployment/src/django/django/core/management/base.py", line
 337, in execute
     output = self.handle(*args, **options)
   File
 "/home/vagrant/deployment/src/django/django/core/management/commands/test.py",
 line 88, in handle
     failures = test_runner.run_tests(test_labels)
   File "/home/vagrant/deployment/src/django/django/test/runner.py", line
 147, in run_tests
     old_config = self.setup_databases()
   File "/home/vagrant/deployment/src/django/django/test/runner.py", line
 109, in setup_databases
     return setup_databases(self.verbosity, self.interactive, **kwargs)
   File "/home/vagrant/deployment/src/django/django/test/runner.py", line
 299, in setup_databases
     serialize=connection.settings_dict.get("TEST_SERIALIZE", True),
   File
 "/home/vagrant/deployment/src/django/django/db/backends/creation.py", line
 373, in create_test_db
     test_database=True,
   File
 "/home/vagrant/deployment/src/django/django/core/management/__init__.py",
 line 115, in call_command
     return klass.execute(*args, **defaults)
   File
 "/home/vagrant/deployment/src/django/django/core/management/base.py", line
 337, in execute
     output = self.handle(*args, **options)
   File
 
"/home/vagrant/deployment/src/django/django/core/management/commands/migrate.py",
 line 146, in handle
     executor.migrate(targets, plan, fake=options.get("fake", False))
   File
 "/home/vagrant/deployment/src/django/django/db/migrations/executor.py",
 line 62, in migrate
     self.apply_migration(migration, fake=fake)
   File
 "/home/vagrant/deployment/src/django/django/db/migrations/executor.py",
 line 96, in apply_migration
     migration.apply(project_state, schema_editor)
   File
 "/home/vagrant/deployment/src/django/django/db/migrations/migration.py",
 line 107, in apply
     operation.database_forwards(self.app_label, schema_editor,
 project_state, new_state)
   File
 
"/home/vagrant/deployment/src/django/django/db/migrations/operations/models.py",
 line 238, in database_forwards
     getattr(new_model._meta, "unique_together", set()),
   File
 "/home/vagrant/deployment/src/django/django/db/backends/sqlite3/schema.py",
 line 169, in alter_unique_together
     self._remake_table(model, override_uniques=new_unique_together)
   File
 "/home/vagrant/deployment/src/django/django/db/backends/sqlite3/schema.py",
 line 126, in _remake_table
     self.execute(sql.replace(temp_model._meta.db_table,
 model._meta.db_table))
   File "/home/vagrant/deployment/src/django/django/db/backends/schema.py",
 line 98, in execute
     cursor.execute(sql, params)
   File "/home/vagrant/deployment/src/django/django/db/backends/utils.py",
 line 65, in execute
     return self.cursor.execute(sql, params)
   File "/home/vagrant/deployment/src/django/django/db/utils.py", line 94,
 in __exit__
     six.reraise(dj_exc_type, dj_exc_value, traceback)
   File "/home/vagrant/deployment/src/django/django/db/backends/utils.py",
 line 65, in execute
     return self.cursor.execute(sql, params)
   File
 "/home/vagrant/deployment/src/django/django/db/backends/sqlite3/base.py",
 line 485, in execute
     return Database.Cursor.execute(self, query, params)
 django.db.utils.OperationalError: index dropbox__dropboxaccount_e8701ad4
 already exists
 }}}

 As far as I can tell, the issue is that Django is creating two
 identically-named indices: one for the ForeignKey and one for the
 unique_together constraint. This issue does not occur if I add
 db_index=False to the ForeignKey or remove the unique_together constraint.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/22844>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/053.41101447981939ae10ff7d0235a65efe%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to