Opened 8 years ago

Last modified 4 years ago

#26624 new Bug

Error when running sqlmigrate after dropping index (of index_together) without actually migrating

Reported by: Akshesh Doshi Owned by: <default>
Component: Migrations Version: dev
Severity: Normal Keywords: sqlmigrate db-indexes
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Akshesh Doshi)

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:

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 makemigrations.
Then see the sql statements using python sqlmigrate <app_name> 0001_initial.
Everything works fine.

2) Remove the index_together part (and the whole Meta class).

Now run python makemigrations.
Then see the sql statements using python sqlmigrate <app_name> <migration_name> and an error is thrown.

Traceback (most recent call last):
  File "", line 22, in <module>
  File "d:\git\django\django\core\management\", line 367, in execute_from_command_line
  File "d:\git\django\django\core\management\", line 359, in execute
  File "d:\git\django\django\core\management\", line 305, in run_from_argv
    self.execute(*args, **cmd_options)
  File "d:\git\django\django\core\management\commands\", line 33, in execute
    return super(Command, self).execute(*args, **options)
  File "d:\git\django\django\core\management\", line 356, in execute
    output = self.handle(*args, **options)
  File "d:\git\django\django\core\management\commands\", line 62, in handle
    sql_statements = executor.collect_sql(plan)
  File "d:\git\django\django\db\migrations\", line 177, in collect_sql
    state = migration.apply(state, schema_editor, collect_sql=True)
  File "d:\git\django\django\db\migrations\", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "d:\git\django\django\db\migrations\operations\", line 557, in database_forwards
    getattr(new_model._meta, self.option_name, set()),
  File "d:\git\django\django\db\backends\base\", 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\", 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).

Change History (4)

comment:1 by Akshesh Doshi, 8 years ago

Description: modified (diff)

comment:2 by Tim Graham, 8 years ago

Keywords: db-indexes added; index removed
Triage Stage: UnreviewedAccepted

Traceback is for PostgreSQL. Seems to work on SQLite, but not on MySQL.

comment:3 by Mariusz Felisiak, 4 years ago

#31834 is a duplicate for modifying an index_together.

comment:4 by Simon Charette, 4 years ago

It was also reported in #31834 that issue is reproducible on SQLite.

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