Oli
Oli

Reputation: 239880

Recreate the tables for a single Django 1.7 app

Many moons ago I used commands like ./manage.py reset appname to DROP and then recreate the database tables for a single App. This was handy for when other developers had inadvertently but manually broken something in the database and you wanted to reset things back without affecting other apps (or needing to go through a lengthy dump/load process).

The advent of Django 1.7 and its builtin migrations support seems to have removed and renamed a lot of these commands and I'm going crosseyed with all the shared prefixes in the documentation. Can somebody spell this out for me?

How do I reset the tables for a single application (one with migrations)?

Upvotes: 1

Views: 516

Answers (1)

spectras
spectras

Reputation: 13552

If your Django migration subsystem is not broken in itself, the normal way to reset an app is to run manage.py migrate <app> zero.

This will run all of the app's migrations backwards, so a few things are noteworthy:

  • if some of the app's migrations are not reversible, the process will fail. Should not happen normally as Django only creates reversible migrations. You can build irreversible ones yourself, though - usually when you create data migrations.

  • if some other app has a dependency on this app, it will also be migrated backwards up to the last migration that did not depend on it.

You can then run migrate again, so it is run forwards.

In any case, remember migrations introduce a risk for your data, so backup your database before touching anything.

Upvotes: 2

Related Questions