[Django] #35022: migration fails from index_together to models.Index

21 views
Skip to first unread message

Django

unread,
Dec 6, 2023, 9:33:43 AM12/6/23
to django-...@googlegroups.com
#35022: migration fails from index_together to models.Index
---------------------------------------------+------------------------
Reporter: Thomas Capricelli | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 5.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------------+------------------------
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.

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

Django

unread,
Dec 6, 2023, 11:51:21 AM12/6/23
to django-...@googlegroups.com
#35022: migration fails from index_together to models.Index
-----------------------------------+--------------------------------------

Reporter: Thomas Capricelli | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: 5.0
Severity: Normal | Resolution: worksforme

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by Natalia Bidart):

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


Comment:

Hello, thank you for your report.

I have tried to reproduce this with a minimal example using the data you
showed above, but I can't reproduce (I used Django 4.2 since that's the
version where the deprecation started, and I also tested on Django 5.0).
Questions:

1. Did you modified your models.py, generated the migration and migrated
using the same Django version? If yes, which one?
2. You refer to a `unique_together` index which you haven't provided any
details for, could you please share minimal but complete models.py to
reproduce the issue?

Lastly, if you are unsure about whether your models are doing the right
thing or not, you could also seek assistance from one of the Django
support channels where there are many friendly people that can help you:
https://www.djangoproject.com/community/

Closing as `worksforme` for now, until further details are provided.

--
Ticket URL: <https://code.djangoproject.com/ticket/35022#comment:1>

Django

unread,
Dec 6, 2023, 2:16:10 PM12/6/23
to django-...@googlegroups.com
#35022: migration fails from index_together to models.Index
-----------------------------------+--------------------------------------
Reporter: Thomas Capricelli | Owner: nobody
Type: Bug | Status: new

Component: Migrations | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by Thomas Capricelli):

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


Comment:

I created a minimal exemple. All of this is done using django 4.2 (but i'm
pretty sure it does the same with django 5.0):

How to use:
./manage.py makemigrations main && ./manage.py migrate # create initial
migrations
vi main/models.py # comment index_together, uncomment indexes =
./manage.py makemigrations main && ./manage.py migrate # crash

--
Ticket URL: <https://code.djangoproject.com/ticket/35022#comment:2>

Django

unread,
Dec 6, 2023, 2:16:46 PM12/6/23
to django-...@googlegroups.com
#35022: migration fails from index_together to models.Index
-----------------------------------+--------------------------------------
Reporter: Thomas Capricelli | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by Thomas Capricelli):

* Attachment "django35022.tgz" added.

minimal example for reproducing the bug

Reply all
Reply to author
Forward
0 new messages