Django 1.7 migrations won't recreate a dropped table, why?

前端 未结 10 1040
挽巷
挽巷 2020-12-23 14:16

Using Django 1.7 migrations.

I accidentally dropped a table in my database. I assumed that by running migration again this would recreate the table but no, Django st

10条回答
  •  -上瘾入骨i
    2020-12-23 14:43

    Probably the simplest way to do it.

    1. Make a copy of the migration file that you want to migrate and rename it as the latest migration file

    e.g. if the file you want to migrate is app_name.002_xyz, and your latest migration file is app_name.004_abc

    Then you need to make a copy of app_name.002_xyz and rename it as the latest migration file. For example, let's rename it to app_name.005_xyz

    1. Now add the recent latest as a dependency in this new file

    e.g. add this line to the new migration file

    class Migration(migrations.Migration):
    
        dependencies = [
            ('app_name', 'app_name.004_abc'),
        ]
        
        ...
    
    1. Migrate it

    e.g. add this line to the new migration file

    python manage.py migrate app_name
    
    1. Awesome, the new migrations are made and tables are recreated

    e.g.

    Running migrations:
    Applying app_name.005_xyz...OK
    
    1. Delete the new migration file app_name.005_xyz and you're good!

提交回复
热议问题