Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complete django DB reset

Tags:

How do I completely reset my Django (1.2 alpha) DB (dropping all tables, rather than just clearing them)?

manage.py flush does too little (won't work if there are schema changes) and manage.py reset requires me to specify all apps (and appears to take a format that is different from just " ".join(INSTALLED_APPS)). I can obviously achieve this in a DB specific way, but I figured there must be a sane, DB backend agnostic way to do this.

[Edit: I'm looking for something that I can call from a script, e.g. a Makefile and that continues to work if I change the backend DB or add to settings.INSTALLED_APPS]

like image 447
as. Avatar asked Feb 18 '10 14:02

as.


People also ask

How do I reset Django migrations?

Reset the Whole Database in Djangosqlite3 and then delete all the migrations folders inside all the apps. After deleting the migrations folders, we can remake the migrations and migrate them using two commands; namely, python manage.py makemigrations and python manage.py migrate .

What is Django default database?

By default, the configuration uses SQLite. If you're new to databases, or you're just interested in trying Django, this is the easiest choice. SQLite is included in Python, so you won't need to install anything else to support your database.


2 Answers

This Snippet gives you the code for a manage.py reset_db command you can install, it's the minimum change that solves your problem


That said, from comments below:

You might as well just install Django command extensions to get reset_db and other goodies: https://github.com/django-extensions/django-extensions

like image 79
Zach Avatar answered Oct 03 '22 22:10

Zach


You want sqlreset:

% python manage.py help sqlreset
Usage: manage.py sqlreset [options] <appname appname ...>

Prints the DROP TABLE SQL, then the CREATE TABLE SQL, for the given app name(s).

Options:
  -v VERBOSITY, --verbosity=VERBOSITY
                        Verbosity level; 0=minimal output, 1=normal output,
                        2=all output
  --settings=SETTINGS   The Python path to a settings module, e.g.
                        "myproject.settings.main". If this isn't provided, the
                        DJANGO_SETTINGS_MODULE environment variable will be
                        used.
  --pythonpath=PYTHONPATH
                        A directory to add to the Python path, e.g.
                        "/home/djangoprojects/myproject".
  --traceback           Print traceback on exception
  --version             show program's version number and exit
  -h, --help            show this help message and exit

Just like when you modify a model, Django will not automatically do this for you. It will only output the commands for you to copy and paste.

like image 35
jathanism Avatar answered Oct 03 '22 21:10

jathanism