Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#22844 closed Bug (fixed)

SQLite3 migrations fail with unique_together on a ForeignKey

Reported by: fongandrew Owned by: Andrew Godwin
Component: Migrations Version: 1.7-beta-2
Severity: Release blocker Keywords: migrations, sqlite
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Change History (4)

comment:1 by Tim Graham, 10 years ago

Component: UncategorizedMigrations
Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

I can reproduce on the latest master.

comment:2 by Andrew Godwin, 10 years ago

Owner: changed from nobody to Andrew Godwin
Status: newassigned

Looking at this now.

comment:3 by Andrew Godwin <andrew@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In d4305a15c1d48a0248ce1da1989a145009029e6c:

[1.7.x] Fixed #22844: Duplicate SQL for SQLite FKs

comment:4 by anonymous, 10 years ago

Disclaimer: This comment is mainly for South users who arrive here after searching Google for django migrate sqlite3 unique_together

This is the only reference I can find of this anywhere, other than a couple of five year old bug report on South (http://south.aeracode.org/ticket/144 and http://south.aeracode.org/ticket/288) and #21236 which seems related to this patch.

The only solution that I could find for this using South-0.8.4 and Django-1.6.5 was to remove the unique_together Meta option, migrate, add the changes to the model, migrate, and the re-add unique_together and migrate one last time.

Note: See TracTickets for help on using tickets.
Back to Top