Search code examples
pythondjangodatabase-migrationdjango-1.7

Django 1.7 - Accidentally Dropped One Table. How To Recover It?


I have accidentally dropped a table in Django 1.7 project. I ran makemigrations & migrate. Both commands didn't recognized that table has dropped. So they had no affect.

Should I remove code for the model, make migration, add the code for the model & again migrate? Or is there a better way to recover it?


Solution

  • Try this:

    python manage.py sqlmigrate app_name 0001 | python manage.py dbshell
    

    It pipes the output of the initial app migration to dbshell, which executes it. Split it up in two steps and copy/paste the SQL commands if you'd like more control over what's happening.

    Naturally the migration contains a single transaction for all the app's tables, so if it is only a single table that is missing (from a multi-model app) you'd have to manually pick only the table you want to recreate.