﻿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
26624	Error when running sqlmigrate after dropping index (of index_together) without actually migrating	Akshesh Doshi	<default>	"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.

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)."	Bug	new	Migrations	dev	Normal		sqlmigrate index		Unreviewed	0	0	0	0	0	0
