Steps to reproduce the bug:
1) Create app with the following models:
{{{
#!python
from django.db import models
class Brand(models.Model):
name = models.CharField(max_length=100, db_index=True)
value = models.IntegerField(blank=True, null=True, default=0)
class Meta:
index_together = (
('name', 'value'),
)
}}}
Run `python manage.py makemigrations`.
Then see the sql statements using `python manage.py sqlmigrate <app_name>
0001_initial`.
Everything works fine.
2) Remove the `index_together` part (and the whole `Meta` class).
Now run `python manage.py makemigrations`.
Then see the sql statements using `python manage.py sqlmigrate <app_name>
<migration_name>` and an error is thrown.
{{{
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "d:\git\django\django\core\management\__init__.py", line 367, in
execute_from_command_line
utility.execute()
File "d:\git\django\django\core\management\__init__.py", line 359, in
execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "d:\git\django\django\core\management\base.py", line 305, in
run_from_argv
self.execute(*args, **cmd_options)
File "d:\git\django\django\core\management\commands\sqlmigrate.py", line
33, in execute
return super(Command, self).execute(*args, **options)
File "d:\git\django\django\core\management\base.py", line 356, in
execute
output = self.handle(*args, **options)
File "d:\git\django\django\core\management\commands\sqlmigrate.py", line
62, in handle
sql_statements = executor.collect_sql(plan)
File "d:\git\django\django\db\migrations\executor.py", line 177, in
collect_sql
state = migration.apply(state, schema_editor, collect_sql=True)
File "d:\git\django\django\db\migrations\migration.py", line 129, in
apply
operation.database_forwards(self.app_label, schema_editor, old_state,
project_state)
File "d:\git\django\django\db\migrations\operations\models.py", line
557, in database_forwards
getattr(new_model._meta, self.option_name, set()),
File "d:\git\django\django\db\backends\base\schema.py", line 345, in
alter_index_together
self._delete_composed_index(model, fields, {'index': True},
self.sql_delete_index)
File "d:\git\django\django\db\backends\base\schema.py", line 358, in
_delete_composed_index
", ".join(columns),
ValueError: Found wrong number (0) of constraints for ert_brand(name,
value)
}}}
But the problem gets rectified if we migrate the first migration (i.e.
0001_initial in this case).
--
Ticket URL: <https://code.djangoproject.com/ticket/26624>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_docs: => 0
* needs_tests: => 0
* needs_better_patch: => 0
Old description:
New description:
I get an error when I run `sqlmigrate` for a migration which drops an
`index_together` constraint until actually migrating the previous
migration. It happens for postgres and not for sqlite.
{{{
--
--
Ticket URL: <https://code.djangoproject.com/ticket/26624#comment:1>
* keywords: sqlmigrate index => sqlmigrate db-indexes
* stage: Unreviewed => Accepted
Comment:
Traceback is for PostgreSQL. Seems to work on SQLite, but not on MySQL.
--
Ticket URL: <https://code.djangoproject.com/ticket/26624#comment:2>
* keywords: sqlmigrate db-indexes => sqlmigrate db-indexes sqlite
--
Ticket URL: <https://code.djangoproject.com/ticket/26624#comment:3>
Comment (by felixxm):
#31834 is a duplicate for modifying an `index_together`.
--
Ticket URL: <https://code.djangoproject.com/ticket/26624#comment:3>
Comment (by Simon Charette):
It was also reported in #31834 that issue is reproducible on SQLite.
--
Ticket URL: <https://code.djangoproject.com/ticket/26624#comment:4>