109

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?

the
  • 21,007
  • 11
  • 68
  • 101
Mirage
  • 30,868
  • 62
  • 166
  • 261

5 Answers5

206

If you don't care about data:

Best way would be to drop the database and run syncdb again. Or you can run:

For Django >= 1.5

python manage.py flush

For Django < 1.5

python manage.py reset appname

(you can add --no-input to the end of the command for it to skip the interactive prompt.)

If you do care about data:

From the docs:

syncdb will only create tables for models which have not yet been installed. It will never issue ALTER TABLE statements to match changes made to a model class after installation. Changes to model classes and database schemas often involve some form of ambiguity and, in those cases, Django would have to guess at the correct changes to make. There is a risk that critical data would be lost in the process.

If you have made changes to a model and wish to alter the database tables to match, use the sql command to display the new SQL structure and compare that to your existing table schema to work out the changes.

https://docs.djangoproject.com/en/dev/ref/django-admin/

Reference: FAQ - https://docs.djangoproject.com/en/dev/faq/models/#if-i-make-changes-to-a-model-how-do-i-update-the-database

People also recommend South ( http://south.aeracode.org/docs/about.html#key-features ), but I haven't tried it.

Chris Moschini
  • 36,764
  • 19
  • 160
  • 190
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • 4
    South is really, really good for automatically handling migrations. Takes a bit of getting used to, but it saves a lot of hassle dropping and recreating databases during development, and is a must-have once your site is live and you need to alter the db structure while you have precious data in it. – thepeer Jun 08 '13 at 14:57
  • 11
    **RESET COMMAND DEPRECIATED**. In Django 1.5 the command has been updated to `flush`. try using `python manage.py flush` or `python manage.py flush --noinput` to skip the interactive prompt. – agconti Jul 20 '13 at 02:33
  • 2
    **South has been deprecated**. Refer [here](https://south.aeracode.org/). And the link you've given here is broken. – Mukesh Sai Kumar Feb 29 '20 at 13:01
  • It looks like the command is now `django-admin flush` according to the [docs](https://docs.djangoproject.com/en/3.0/ref/django-admin/#flush) – Todd May 01 '20 at 21:02
6

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).

mrmagooey
  • 4,832
  • 7
  • 37
  • 49
4

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:

  1. Drop the database from MySQL
  2. Recreate the database
  3. Run python manage.py migrate
Anupam
  • 14,950
  • 19
  • 67
  • 94
  • 2
    It would be useful to add commands for steps 1 and 2. – Floella Mar 05 '23 at 02:55
  • @Floella those commands need to be run directly on database - so, outside the scope of Django. If you are asking how to drop/create the database on MySQL, those are usual SQL: `DROP DATABASE ` and `CREATE DATABASE ` – Anupam Mar 08 '23 at 16:58
0

You can use the Django-Truncate library to delete all data of a table without destroying the table structure.

Example:

  1. First, install django-turncate using your terminal/command line:
pip install django-truncate
  1. Add "django_truncate" to your INSTALLED_APPS in the settings.py file:
INSTALLED_APPS = [
    ...
    'django_truncate',
]
  1. Use this command in your terminal to delete all data of the table from the app.
python manage.py truncate --apps app_name --models table_name
Mahbub Ul Islam
  • 773
  • 8
  • 15
0

Quickest (drops and creates all tables including data):

./manage.py reset appname | ./manage.py dbshell

Caution:

  • Might not work on Windows correctly.
  • Might keep some old tables in the db
Udi
  • 29,222
  • 9
  • 96
  • 129
  • 3
    `reset` already runs the SQL, so no need to pipe to `dbshell`. If you use the `sqlreset` command, then that would just print out the SQL, which you could pipe to the shell for execution, but it's an unnecessary step. – Michael Mior Mar 11 '13 at 14:13