9

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?

Chillar Anand
  • 27,936
  • 9
  • 119
  • 136
  • 1
    I would probably use the `django-admin sql` command and recreate the table manually, but did you try to use `migrate` to go back to a version prior to the creation of that table, and then migrating again to the latest version? See also the `--fake` switch to `migrate`, but I don't know if it would be useful here. – Paulo Almeida Sep 08 '14 at 13:43
  • 1
    Unless you have backup or fixtures, sorry to say that your data is gone. You can restore the database tables, but not the contents. – karthikr Sep 08 '14 at 14:18

1 Answers1

20

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.

Simon
  • 3,667
  • 1
  • 35
  • 49