﻿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
35022	RenameIndex crashes when there is a unique index on the same fields.	Thomas Capricelli	David Wobrock	"As index_together is obsolated, i changed in my models.py:

{{{
-        index_together = [
-            ('name', 'host',)
-        ]
+        indexes = [models.Index(fields=[""name"", ""host""])]
}}}

makemigrations created what seems a legit migration:

{{{
# Generated by Django 5.0.1 on 2023-12-06 12:46

from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('main', '0004_auto_20200327_0040'),
    ]

    operations = [
        migrations.RenameIndex(
            model_name='list',
            new_name='main_list_name_cbdf64_idx',
            old_fields=('name', 'host'),
        ),
    ]
}}}

But the actual migration fails badly:

{{{
% ./manage.py  migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, flatpages, main, registration, sessions, sites
Running migrations:
  Applying main.0005_rename_list_name_host_main_list_name_cbdf64_idx...
Traceback (most recent call last):
  File ""/home/orzel/hg/xxx/./manage.py"", line 8, in <module>
    execute_from_command_line(sys.argv)
  File ""/usr/lib/python3.11/site-packages/django/core/management/__init__.py"", line 442, in execute_from_command_line
    utility.execute()
  File ""/usr/lib/python3.11/site-packages/django/core/management/__init__.py"", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File ""/usr/lib/python3.11/site-packages/django/core/management/base.py"", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File ""/usr/lib/python3.11/site-packages/django/core/management/base.py"", line 458, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ""/usr/lib/python3.11/site-packages/django/core/management/base.py"", line 106, in wrapper
    res = handle_func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ""/usr/lib/python3.11/site-packages/django/core/management/commands/migrate.py"", line 356, in handle
    post_migrate_state = executor.migrate(
                         ^^^^^^^^^^^^^^^^^
  File ""/usr/lib/python3.11/site-packages/django/db/migrations/executor.py"", line 135, in migrate
    state = self._migrate_all_forwards(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ""/usr/lib/python3.11/site-packages/django/db/migrations/executor.py"", line 167, in _migrate_all_forwards
    state = self.apply_migration(
            ^^^^^^^^^^^^^^^^^^^^^
  File ""/usr/lib/python3.11/site-packages/django/db/migrations/executor.py"", line 252, in apply_migration
    state = migration.apply(state, schema_editor)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ""/usr/lib/python3.11/site-packages/django/db/migrations/migration.py"", line 132, in apply
    operation.database_forwards(
  File ""/usr/lib/python3.11/site-packages/django/db/migrations/operations/models.py"", line 1050, in database_forwards
    raise ValueError(
ValueError: Found wrong number (2) of indexes for main_list(name, host).
}}}


I'm currently testing and the database is sqlite3. Browsing the sqlite file i can see 3 indices on this table: one unrelated (id) and two for name_host : one ""idx"" and one ""uniq"".

My bet is that having both an index_together and a unique_together is ... bad ? (my bad?).
Or maybe it just freaks out django ? It might be specific to sqlite.




"	Bug	closed	Migrations	5.0	Normal	fixed		David Wobrock	Ready for checkin	1	0	0	0	0	0
