﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
22844	SQLite3 migrations fail with unique_together on a ForeignKey	fongandrew	Andrew Godwin	"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.
"	Bug	closed	Migrations	1.7-beta-2	Release blocker	fixed	migrations, sqlite		Accepted	0	0	0	0	0	0
