user2134226 user2134226 - 2 years ago 107
SQL Question

What is the easiest way to clear a database from the CLI with in Django?

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 don't touch existing tables. So is there any better way to handle this problem

Answer Source

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 flush

For Django < 1.5

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

Reference: FAQ -

People also recommend South ( ), but I haven't tried it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download