#26624: Error when running sqlmigrate after dropping index (of index_together)
without actually migrating
----------------------------------+--------------------------------------
     Reporter:  akki              |                    Owner:  <default>
         Type:  Bug               |                   Status:  new
    Component:  Migrations        |                  Version:  master
     Severity:  Normal            |               Resolution:
     Keywords:  sqlmigrate index  |             Triage Stage:  Unreviewed
    Has patch:  0                 |      Needs documentation:  0
  Needs tests:  0                 |  Patch needs improvement:  0
Easy pickings:  0                 |                    UI/UX:  0
----------------------------------+--------------------------------------
Changes (by akki):

 * needs_docs:   => 0
 * needs_tests:   => 0
 * needs_better_patch:   => 0


Old description:

> I get an error when I run `sqlmigrate` for a migration which drops an
> `index_together` constraint until actually migrating the previous
> migration.
>
> 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).

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.

 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#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/062.91e6413f821c8cf0a402e7a4ceb812c8%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to