[Django] #22844: SQLite3 migrations fail with unique_together on a ForeignKey

13 views
Skip to first unread message

Django

unread,
Jun 15, 2014, 6:48:53 PM6/15/14
to django-...@googlegroups.com
#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.

Django

unread,
Jun 15, 2014, 7:10:11 PM6/15/14
to django-...@googlegroups.com
#22844: SQLite3 migrations fail with unique_together on a ForeignKey
------------------------------------+--------------------------------------
Reporter: fongandrew | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.7-beta-2
Severity: Release blocker | Resolution:
Keywords: migrations, sqlite | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
------------------------------------+--------------------------------------
Changes (by timo):

* severity: Normal => Release blocker
* needs_better_patch: => 0
* component: Uncategorized => Migrations
* needs_tests: => 0
* needs_docs: => 0
* type: Uncategorized => Bug
* stage: Unreviewed => Accepted


Comment:

I can reproduce on the latest master.

--
Ticket URL: <https://code.djangoproject.com/ticket/22844#comment:1>

Django

unread,
Jun 16, 2014, 2:55:50 AM6/16/14
to django-...@googlegroups.com
#22844: SQLite3 migrations fail with unique_together on a ForeignKey
-------------------------------------+-------------------------------------
Reporter: fongandrew | Owner:
Type: Bug | andrewgodwin
Component: Migrations | Status: assigned
Severity: Release blocker | Version:
Keywords: migrations, sqlite | 1.7-beta-2
Has patch: 0 | Resolution:
Needs tests: 0 | Triage Stage: Accepted
Easy pickings: 0 | Needs documentation: 0
| Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by andrewgodwin):

* status: new => assigned
* owner: nobody => andrewgodwin


Comment:

Looking at this now.

--
Ticket URL: <https://code.djangoproject.com/ticket/22844#comment:2>

Django

unread,
Jun 16, 2014, 3:28:13 AM6/16/14
to django-...@googlegroups.com
#22844: SQLite3 migrations fail with unique_together on a ForeignKey
-------------------------------------+-------------------------------------
Reporter: fongandrew | Owner:
Type: Bug | andrewgodwin
Component: Migrations | Status: closed

Severity: Release blocker | Version:
Keywords: migrations, sqlite | 1.7-beta-2
Has patch: 0 | Resolution: fixed

Needs tests: 0 | Triage Stage: Accepted
Easy pickings: 0 | Needs documentation: 0
| Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Andrew Godwin <andrew@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"d4305a15c1d48a0248ce1da1989a145009029e6c"]:
{{{
#!CommitTicketReference repository=""
revision="d4305a15c1d48a0248ce1da1989a145009029e6c"
[1.7.x] Fixed #22844: Duplicate SQL for SQLite FKs
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/22844#comment:3>

Django

unread,
Jun 26, 2014, 7:09:40 PM6/26/14
to django-...@googlegroups.com
#22844: SQLite3 migrations fail with unique_together on a ForeignKey
-------------------------------------+-------------------------------------
Reporter: fongandrew | Owner:
Type: Bug | andrewgodwin
Component: Migrations | Status: closed
Severity: Release blocker | Version:
Keywords: migrations, sqlite | 1.7-beta-2
Has patch: 0 | Resolution: fixed
Needs tests: 0 | Triage Stage: Accepted
Easy pickings: 0 | Needs documentation: 0
| Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by anonymous):

**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.

--
Ticket URL: <https://code.djangoproject.com/ticket/22844#comment:4>

Reply all
Reply to author
Forward
0 new messages