Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset SQLite database in Django

I am trying to refactor a Django project. I renamed a couple apps and added a new one, as well as shuffled some models around. I want to clear my database and migrations and start fresh, but I am not sure how to accomplish this. Here's 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:

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

Can anyone tell me what I might be doing wrong?

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

like image 677
InnisBrendan Avatar asked May 07 '16 01:05

InnisBrendan


People also ask

How do I clear data in SQLite?

sqlite> DELETE FROM table_name; Following is the basic syntax of DROP TABLE. sqlite> DROP TABLE table_name; If you are using DELETE TABLE command to delete all the records, it is recommended to use VACUUM command to clear unused space.

How delete all data Django?

If you want to remove all the data from all your tables, you might want to try the command python manage.py flush . This will delete all of the data in your tables, but the tables themselves will still exist. Save this answer.

How do I reset Django migrations?

Django's migration can be reset by cleaning all the migration files except __init__.py files under each project app directory, followed by dropping the database and creating migration again using python manage.py makemigrations and python manage.py migrate .


1 Answers

Delete database and delete migration files (.py and .pyc) in migrations directory of your app (don't delete __init__.py file). Then run python manage.py makemigrations app and python manage.py migrate.

like image 180
vsd Avatar answered Sep 24 '22 21:09

vsd