3

So I dropped a table in my database, and I want it back. Rerunning a migration gave errors table didn't exist. After some hunting I learned I could remove everything in my django_migrations that had app name my app. So i did that, reran migrations it started to work then griped about tables that were not dropped.

I have no idea how to just get the one table back again and keep everything as it was... anyway to do this?

I don't care about 90% of the data in the database, only a few tables and fields. The table i dropped i cared nothing about the data.

Codejoy
  • 3,722
  • 13
  • 59
  • 99

2 Answers2

5

You can use the sqlmigrate command to print the SQL commands that Django would execute. Then you can pick the commands necessary to recreate the tables you removed and apply them manually.

Once all the tables are in place again, you can use migrate <app_name> --fake to forward the migration history, so Django knows that the migrations in this app are applied.

knbk
  • 52,111
  • 9
  • 124
  • 122
  • ran this django-admin sqlmigrate swsite mymigration it gave an error: django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. sigh. All this because postgres had issues with a timezone. – Codejoy Jan 19 '17 at 17:08
  • 1
    Just run `python manage.py sqlmigrate`. You can use `django-admin`, but then you need to explicitly configure the settings module. – knbk Jan 19 '17 at 17:09
  • that command says not enough arguments. It wants more like the app name and the migration name i guess I can run them individually? – Codejoy Jan 19 '17 at 17:28
3

Start by inspecting the django_migrations table. It contains data about which migrations have already run. Compare this data to the actual migrations files to learn which ones have run or not.

Finally, don't be afraid to delete rows in django_migrations and modify your original migrations files to recreate the tables you need.

Daniel van Flymen
  • 10,931
  • 4
  • 24
  • 39