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?

2
  • 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. Commented Sep 8, 2014 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
    Commented Sep 8, 2014 at 14:18

1 Answer 1

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.

2
  • Good old syncdb used to be much better at handling scenario such as this.
    – fmalina
    Commented Feb 24, 2016 at 15:48
  • This was so helpful!
    – Kewal Shah
    Commented Dec 29, 2018 at 3:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.