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

前端 未结 10 1039
挽巷
挽巷 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条回答
  •  时光取名叫无心
    2020-12-23 14:52

    Migrations check for differences in your models, then translates that to actions, which are translated to SQL. It does not automatically sync the db scheme with your models, and it has no way of knowing you dropped a table (it doesn't know about manual changes because, well, you're not supposed to do manual changes. That's the point)

    The answer? a manual change requires a manual migration as well. What you need to do is simply write your own migration and manually tell south to re-build the table. It's not very difficult, The docs make it pretty easy. Just make something like this:

    from django.db import migrations, models
    
    class Migration(migrations.Migration):
    
        operations = [
            migrations.CreateModel("Foo"),
            migrations.AddField("Foo", "bar", models.IntegerField(default=0))
        ] 
    

    You can probably look into the first migration file (the one that made the model in the first place) and copy paste almost all of it. Then all you have to do is run the migration like you always do

提交回复
热议问题