[Django] #27946: Removing unique_together creates invalid migration

472 views
Skip to first unread message

Django

unread,
Mar 16, 2017, 7:38:04 AM3/16/17
to django-...@googlegroups.com
#27946: Removing unique_together creates invalid migration
-----------------------------------------+------------------------
Reporter: Robin Elvin | Owner: nobody
Type: Uncategorized | Status: new
Component: Migrations | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
I had a model which had a unique_together constraint defined. Now I wish
to remove this so the unique_together was removed from the model and a
migration created. When applying the migration the following error
occurred:

{{{
Applying main.0091_auto_20170316_1046...Traceback (most recent call
last):
File "manage.py", line 8, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/site-
packages/django/core/management/__init__.py", line 354, in
execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/site-
packages/django/core/management/__init__.py", line 346, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/site-
packages/django/core/management/base.py", line 394, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/site-
packages/django/core/management/base.py", line 445, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/site-
packages/django/core/management/commands/migrate.py", line 222, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/site-
packages/django/db/migrations/executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake,
fake_initial=fake_initial)
File "/usr/local/lib/python2.7/site-
packages/django/db/migrations/executor.py", line 148, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python2.7/site-
packages/django/db/migrations/migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state,
project_state)
File "/usr/local/lib/python2.7/site-
packages/django/db/migrations/operations/models.py", line 359, in
database_forwards
getattr(new_model._meta, self.option_name, set()),
File "/usr/local/lib/python2.7/site-
packages/django/db/backends/base/schema.py", line 320, in
alter_unique_together
self._delete_composed_index(model, fields, {'unique': True},
self.sql_delete_unique)
File "/usr/local/lib/python2.7/site-
packages/django/db/backends/mysql/schema.py", line 80, in
_delete_composed_index
return super(DatabaseSchemaEditor, self)._delete_composed_index(model,
fields, *args)
File "/usr/local/lib/python2.7/site-
packages/django/db/backends/base/schema.py", line 349, in
_delete_composed_index
", ".join(columns),
ValueError: Found wrong number (0) of constraints for
main_projectdrivefile(name, directory_id)
}}}

The model's Meta before starting which was subsequently removed prior to
creating the migration:

{{{
#!python
class Meta:
unique_together = ('name', 'directory')

}}}

The migration created looks like this:

{{{
#!python
class Migration(migrations.Migration):

dependencies = [
('main', '0090_auto_20170314_1033'),
]

operations = [
migrations.AlterUniqueTogether(
name='projectdrivefile',
unique_together=set([]),
),
]
}}}

Steps to reproduce:

1. Create a model with a unique_together constraint
2. Create a migration for this model with makemigrations
3. Apply migration
4. Remove unique_together completely
5. Create a migration for this model with makemigrations
6. Apply migration

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

Django

unread,
Mar 16, 2017, 7:39:42 AM3/16/17
to django-...@googlegroups.com
#27946: Removing unique_together creates invalid migration
-------------------------------+--------------------------------------

Reporter: Robin Elvin | Owner: nobody
Type: Uncategorized | Status: new
Component: Migrations | Version: 1.8
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by Robin Elvin:

Old description:

New description:

}}}

{{{
#!python
class Migration(migrations.Migration):

Steps to reproduce:

Note: There appears to have been at least 1 report of this bug previously
but the reporter could not recreate so the bug was closed.

--

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

Django

unread,
Mar 16, 2017, 8:08:45 AM3/16/17
to django-...@googlegroups.com
#27946: Removing unique_together creates invalid migration
-----------------------------+--------------------------------------

Reporter: Robin Elvin | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.8
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------------------------
Changes (by Tim Graham):

* type: Uncategorized => Bug


Comment:

Which database are you using? Can you reproduce the issue with Django's
master branch or a version newer than 1.8?

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

Django

unread,
Mar 16, 2017, 8:36:59 AM3/16/17
to django-...@googlegroups.com
#27946: Removing unique_together creates invalid migration
-----------------------------+--------------------------------------

Reporter: Robin Elvin | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.8
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------------------------

Comment (by Robin Elvin):

This is on MySQL.

In the course of trying to recreate this issue I have discovered that the
issue stems from the fact that the table does not have the UNIQUE KEY
currently. I am unsure as to how this has happened but this means that the
table is not consistent with the migrations. I have manually added the
UNIQUE KEY back using ALTER TABLE and successfully applied the migration.

So, I don't know if this is something migrations should handle. Given that
the desired state of the model is in effect what it is currently should it
just ignore the error? Or perhaps the error message should be more
descriptive as this threw me off the scent for quite a while.

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

Django

unread,
Mar 16, 2017, 8:39:12 PM3/16/17
to django-...@googlegroups.com
#27946: Improve "ValueError: Found wrong number of constraints" error message
--------------------------------------+------------------------------------

Reporter: Robin Elvin | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Migrations | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Tim Graham):

* type: Bug => Cleanup/optimization
* stage: Unreviewed => Accepted


Comment:

Perhaps the error message could be improved. I've seen several tickets
reporting this error message as a bug in Django when as you mentioned, it
really has to do with the fact that the database is in an unexpected
state.

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

Django

unread,
Apr 29, 2019, 2:58:10 PM4/29/19
to django-...@googlegroups.com
#27946: Improve "ValueError: Found wrong number of constraints" error message
--------------------------------------+------------------------------------
Reporter: Robin Elvin | Owner: nobody
Type: Cleanup/optimization | Status: closed
Component: Migrations | Version: 1.8
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Fred Palmer):

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


Comment:

This appears to have been fixed by the following:
https://code.djangoproject.com/ticket/29480

--
Ticket URL: <https://code.djangoproject.com/ticket/27946#comment:5>

Reply all
Reply to author
Forward
0 new messages