Reset SQLite Database in Django

I am trying to reorganize the Django project. I renamed a couple of applications and added a new one, and also shuffled some models. I want to clear my database and migrations and start a new job, but I'm not sure how to do this. Here is what I did:

rm -r myapp/migrations // I ran this for all my apps python manage.py flush python manage.py makemigrations myapp // I ran this for all my apps python manage.py migrate // This errors 

I get an error message:

 django.db.utils.OperationalError: table "myapp_mymodel" already exists 

Can someone tell me what I can do wrong?

EDIT: What is the django command to delete all tables? does not work.

+8
source share
4 answers

Delete the database and delete the migration files ( .py and .pyc ) in the migrations directory of your application (do not delete the __init__.py file). Then run python manage.py makemigrations app and python manage.py migrate .

+28
source

I had the same problem using Django 1.10, here's what I did, I deleted the database sqlite file, deleted the pycache folders inside each application, deleted all the files inside the migration folder for each application except the init .py file, and then run python manage.py makemigrations and python manage.py migrate . Also note that since you deleted the database, you will need to create a new superuser using python manage.py createsuperuser . Hope this helps

+2
source

Do not delete the database file!

Deleting the migration files correctly and then running flush, but deleting the sqlite database file is incorrect. It worked for me every time. If you use a different database, this will save you a lot of work and preparation.

  1. delete all files ".py" and ".pyc" manually
  2. python manage.py flush
    enter yes to confirm
  3. python manage.py makemigrations
  4. python manage.py migrate
0
source

Just for me

 python manage.py flush 

deleted the old database contents, so I was able to recreate the entries in Django 2.1.4.

Remember to create a new superuser:

 python manage.py createsuperuser 
0
source

All Articles