TLDR: Models moved to a new app. After migrating M2M relation refers to table that does not exist. Previous M2M table was not renamed.
Django Version: 3.2.3
Scenario:
I am refactoring a Django application. In this process I have moved some models to a new app. To do so, I renamed the tables as described by Haki Benita, using SeparateDatabaseAndState.
One of the models Studentsin the new app has a field with Many2Many relationship to another model Teachers (which I also moved to the new app).
oldapp/migrations/000X_auto_XXX.py
operations = [
...
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.DeleteModel(
name='Students',
)
],
database_operations=[
migrations.AlterModelTable(
name='Students',
table='newapp_students'
)
]
),
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.DeleteModel(
name='Teachers',
)
],
database_operations=[
migrations.AlterModelTable(
name='Teachers',
table='newapp_Teachers'
)
]
),
...
]
newapp/migrations/0001_initial.py
operations = [
...
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.CreateModel(
name='Teacher',
fields=[...],
options={...},
)
],
database_operations=[]
),
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.CreateModel(
name='Students',
fields=[
...
('teachers', models.ManyToManyField(blank=True,related_name='students', to='newapp.Teachers')),
...
],
options={...},
)
],
database_operations=[]
),
...
]
After running python manage.py migrate all the Models got renamed (so far so good)..
Problem: The automatically generated table for Many2Many was not renamed (oldapp_students_teachers). However, the model in the new app refers to a table in the new app (newapp_students_teachers) that does not exist.
I did some reasearch and it seems, that this problem was addresed and fixed several years ago 41b337efa085b6b9cfdb2cf724d977005ff77e75.
However for me the problem still remains. So what am I doing wrong?