I want to rename a model in Django 3.2, keep my existing migrations and be
able to both migrate a db with the old table name and create a db from
scratch.

I've started by renaming the model class and all references to it in the
code. As "./manage.py makemigrations" did not automatically create a
migration, I manually created a migration that renames the model:

from django.db import migrationsclass Migration(migrations.Migration):

    dependencies = [
        ('market_integrations', '0003_migration'),
    ]

    operations = [
        migrations.RenameModel('OldModelName', 'NewModelname')
    ]

My initial idea is that I should not update existing migrations, as I never
do when creating other migrations. However, the references to the old model
in the old migrations cause a LookupError when I run "./manage.py migrate".
I've tried using both the model name string and apps.get_model(). Migration
code samples that break:

operations = [
    migrations.CreateModel(
        name="OldModelName",
    ...
    )
]

operations = [
    migrations.CreateModel(
        name=apps.get_model("myapp", "OldModelName"),
    ...
    )
]

As keeping the old model name in old migrations didn't work, I replaced the
old model name in old migrations with the new name. After doing that,
"./manage.py migrate" ran successfully, including the model renaming
migration. However, when I try to create a new database, the model renaming
migration fails because that new database never had a table with the old
name.

What should I do to be able to preserve my migrations and have them working
in both existing and new databases?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAKz%2BTUs%2B0mNkKB9O%3DTHnpUn0X8dL60P_46%2B8uudjiXt8P5VMXg%40mail.gmail.com.

Reply via email to