Dan
Dan

Reputation: 641

How do I delete DB (sqlite3) in Django 1.9 to start from scratch?

I made a spelling error in my model and now one of my columns is misspelled. I'd like to drop all tables in the database, fix the error in the model.py, and recreate the database with the correct spelling in model.

I've tried to use the suggestions in the this article but the table still exists after I follow the commands outlined there.

Anyone have a quick way to do this?

Upvotes: 48

Views: 80977

Answers (4)

Mohammed Shareef C
Mohammed Shareef C

Reputation: 4050

  1. Delete the sqlite database file (often db.sqlite3) in your django project folder (or wherever you placed it)
  2. Delete everything except __init__.py file from migration folder in all django apps (eg: rm */migrations/0*.py)
  3. Make changes in your models (models.py).
  4. Run the command python manage.py makemigrations or python3 manage.py makemigrations
  5. Then run the command python manage.py migrate.

That's all.

If your changes to the models are not detected by makemigrations command, please check this answer

Upvotes: 105

trex
trex

Reputation: 4057

To do everything in one go, run all below commands in a shell script (Linux flavors) or in a batch file (Windows). and run that script where your manage.py exists:

find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc"  -delete
find . -path "*.sqlite3"  -delete
python manage.py makemigrations
python manage.py migrate
python manage.py migrate
python manage.py shell -c "from django.contrib.auth.models import User; User.objects.create_superuser('admin', '[email protected]', 'adminpass')"

I prefer them running by copy-pasting directly in one go. make sure to change superuser credentials.

Note- This will DELETE all databases and their corresponding migrations

Upvotes: 0

Zaeem
Zaeem

Reputation: 149

rm -f tmp.db db.sqlite3 rm -r my-app/migrations python manage.py makemigrations python manage.py migrate

Removes the database.
Removes the migrations from your app.
Re-runs the migrations. Note: you could also do: python manage.py makemigrations my-app
Migrate changes.

Upvotes: 14

iklinac
iklinac

Reputation: 15758

You can just delete your sqlite file.

Regarding your question, you should use Django migration system to do database changes for you project using makemigrations and migrate commands

migration docs

Upvotes: 0

Related Questions