Opened 9 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 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).

Change History (4)

comment:1 by Akshesh Doshi, 9 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