I am using Django to build a website with MySQL. Now as I am learning so I need to change the Model very often so I want that all tables get cleared and new table get created.
But syncdb
doesn't touch existing tables. Is there any better way to handle this problem?
This question is related to
python
mysql
django
django-1.3
Quickest (drops and creates all tables including data):
./manage.py reset appname | ./manage.py dbshell
Caution:
I think Django docs explicitly mention that if the intent is to start from an empty DB again (which seems to be OP's intent), then just drop and re-create the database and re-run migrate
(instead of using flush
):
If you would rather start from an empty database and re-run all migrations, you should drop and recreate the database and then run migrate instead.
So for OP's case, we just need to:
python manage.py migrate
You can use the Django-Truncate library to delete all data of a table without destroying the table structure.
Example:
pip install django-truncate
settings.py
file:INSTALLED_APPS = [
...
'django_truncate',
]
python manage.py truncate --apps app_name --models table_name
Using Django Extensions, running:
./manage.py reset_db
Will clear the database tables, then running:
./manage.py syncdb
Will recreate them (south may ask you to migrate things).
Source: Stackoverflow.com