[Django] #26624: Error when running sqlmigrate after dropping index (of index_together) without actually migrating

76 views
Skip to first unread message

Django

unread,
May 17, 2016, 7:52:45 AM5/17/16
to django-...@googlegroups.com
#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 | Keywords: sqlmigrate index
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------
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).

--
Ticket URL: <https://code.djangoproject.com/ticket/26624>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 17, 2016, 8:08:10 AM5/17/16
to django-...@googlegroups.com
#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:

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>

Django

unread,
May 26, 2016, 2:23:47 PM5/26/16
to django-...@googlegroups.com
#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 db- | Triage Stage: Accepted
indexes |

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* 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>

Django

unread,
Jun 20, 2016, 10:38:41 AM6/20/16
to django-...@googlegroups.com
#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 db- | Triage Stage: Accepted
indexes sqlite |

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by charettes):

* keywords: sqlmigrate db-indexes => sqlmigrate db-indexes sqlite


--
Ticket URL: <https://code.djangoproject.com/ticket/26624#comment:3>

Django

unread,
Jul 28, 2020, 12:33:35 AM7/28/20
to django-...@googlegroups.com
#26624: Error when running sqlmigrate after dropping index (of index_together)
without actually migrating
-------------------------------------+-------------------------------------
Reporter: Akshesh Doshi | Owner: <default>

Type: Bug | Status: new
Component: Migrations | Version: master
Severity: Normal | Resolution:
Keywords: sqlmigrate db- | Triage Stage: Accepted
indexes |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by felixxm):

#31834 is a duplicate for modifying an `index_together`.

Django

unread,
Jul 28, 2020, 10:52:11 AM7/28/20
to django-...@googlegroups.com
#26624: Error when running sqlmigrate after dropping index (of index_together)
without actually migrating
-------------------------------------+-------------------------------------
Reporter: Akshesh Doshi | Owner: <default>

Type: Bug | Status: new
Component: Migrations | Version: master
Severity: Normal | Resolution:
Keywords: sqlmigrate db- | Triage Stage: Accepted
indexes |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Feb 18, 2025, 3:51:06 PM2/18/25
to django-...@googlegroups.com
#26624: Error when running sqlmigrate after dropping index (of index_together)
without actually migrating
-------------------------------------+-------------------------------------
Reporter: Akshesh Doshi | Owner: <default>
Type: Bug | Status: new
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: sqlmigrate db- | Triage Stage: Accepted
indexes |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* cc: Jacob Walls (added)

--
Ticket URL: <https://code.djangoproject.com/ticket/26624#comment:5>

Django

unread,
Apr 8, 2025, 1:46:18 PM4/8/25
to django-...@googlegroups.com
#26624: Error when running sqlmigrate after dropping index (of index_together)
without actually migrating
-------------------------------------+-------------------------------------
Reporter: Akshesh Doshi | Owner: <default>
Type: Bug | Status: closed
Component: Migrations | Version: dev
Severity: Normal | Resolution: wontfix
Keywords: sqlmigrate db- | Triage Stage: Accepted
indexes |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* resolution: => wontfix
* status: new => closed

Comment:

Now that `index_together` has been deprecated I think we can won't fix
that one.

There's a similar bug opened for `Meta.indexes` in #35595 in and
`constraints` in #35962.
--
Ticket URL: <https://code.djangoproject.com/ticket/26624#comment:6>

Django

unread,
May 21, 2025, 7:27:53 AM5/21/25
to django-...@googlegroups.com
#26624: Error when running sqlmigrate after dropping index (of index_together)
without actually migrating
-------------------------------------+-------------------------------------
Reporter: Akshesh Doshi | Owner: <default>
Type: Bug | Status: closed
Component: Migrations | Version: dev
Severity: Normal | Resolution: wontfix
Keywords: sqlmigrate db- | Triage Stage: Accepted
indexes |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Jacob Walls):

`index_together` is
[https://docs.djangoproject.com/en/dev/ref/models/options/#django.db.models.Options.unique_together
discouraged], but I don't see anywhere that it's been deprecated, and it
will exist in migrations indefinitely.

Where I'm seeing this issue is migrating from a model like this

{{{
class Meta:
unique_together = (("a", "b", "c"))
}}}

to a UniqueConstraint removing one field:
{{{
class Meta:
constraints = [
models.UniqueConstraint(
fields=("a", "b"), name="bug_report"
),
]
}}}

Then `migrate`, then `sqlmigrate` produces `ValueError: Found wrong number
(0) of constraints`, which is not the same as the original report (failure
is after migrate, not before)

If the unique constraint is a straight port (not removing any fields):
{{{
class Meta:
constraints = [
models.UniqueConstraint(
fields=("a", "b", "c"), name="bug_report"
),
]
}}}

... `sqlmigrate` no longer crashes after `migrate`, but does not produce
the correct SQL:

{{{
BEGIN;
--
-- Alter unique_together for cardxnodexwidget (0 constraint(s))
--
ALTER TABLE "table" DROP CONSTRAINT "bug_report"; -- should not say
"bug_report"
--
-- Create constraint jtw on model cardxnodexwidget
--
ALTER TABLE "table" ADD CONSTRAINT "bug_report" UNIQUE ("a", "b", "c");
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26624#comment:7>

Django

unread,
May 21, 2025, 7:32:42 AM5/21/25
to django-...@googlegroups.com
#26624: Error when running sqlmigrate after dropping index (of index_together)
without actually migrating
-------------------------------------+-------------------------------------
Reporter: Akshesh Doshi | Owner: <default>
Type: Bug | Status: closed
Component: Migrations | Version: dev
Severity: Normal | Resolution: wontfix
Keywords: sqlmigrate db- | Triage Stage: Accepted
indexes |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Jacob Walls):

> index_together is ​discouraged, but I don't see anywhere that it's been
deprecated, and it will exist in migrations indefinitely.

My bad, `index_together` is definitely deprecated, whereas
`unique_together` is merely discouraged. I confused them because I came
here from a duplicate for `unique_together`: #31834. comment:7 also deals
with `unique_together`.

I think we ought to undo the wontfix. Not sure whether to retitle this
ticket for `unique_together` or start a new one.
--
Ticket URL: <https://code.djangoproject.com/ticket/26624#comment:8>

Django

unread,
May 21, 2025, 8:01:08 AM5/21/25
to django-...@googlegroups.com
#26624: Error when running sqlmigrate after dropping index (of index_together)
without actually migrating
-------------------------------------+-------------------------------------
Reporter: Akshesh Doshi | Owner: <default>
Type: Bug | Status: closed
Component: Migrations | Version: dev
Severity: Normal | Resolution: wontfix
Keywords: sqlmigrate db- | Triage Stage: Accepted
indexes |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Jacob Walls):

The first part of my report is all but identical to #31834, so I think it
would be simplest to just reopen that ticket and leave this one
wontfix'ed. I'll do that now.
--
Ticket URL: <https://code.djangoproject.com/ticket/26624#comment:9>
Reply all
Reply to author
Forward
0 new messages