How to reset Django database?
Question:
I’d really like to know how to reset Django database.
Specifically, I accidentally deleted a table with this command,
$ python manage.py dbshell
$ DROP TABLE qaapp_question;
and everything messed up.
I followd a stackoverflow post and just deleted the table, and, haha, completely messed up. syncdb stuff is apparently deprecated so cannot use it.
I tried all the ways I googled and found, as followings:
$ python manage.py migrate qaapp zero
django.db.utils.OperationalError: no such table: qaapp_question
Not working.
$ manage.py migrate --fake <appname> zero
$ rm -rf migrations # I deleted the app migrations folder manually
$ manage.py makemigrations <appname>
$ manage.py migrate --fake <appname>
Not working.
$ python manage.py flush
Not working.
$ python manage.py reset_db
Reset successful.
Not working.
manually trying to delete the sqlite database file
-> resource busy or locked
Not working.
I wanted to restore the deleted table, but I don’t care anymore, just kindly tell me how to delete, initialize the database, then be able to rebuild the database with python manage.py makemigrations
and python manage.py migrate
.
Thanks for your help.
Answers:
Simply deleting the SQlite database file should work. If you get "resource busy or locked" simply copy the entire project directory to somewhere else and try to delete it. Think of it, will you upload your database file altogether to heroku when deploying? I know it’s No, SQlite files should not be included to the git project and should be included in the .gitignore file. So, if the database file is nowhere to be found, Django will autogenerates a new database (reset) for you.
Simply run the command
python manage.py flush
And answer the following question with ‘yes’
This will IRREVERSIBLY DESTROY all data currently in the "qaapp" database,
and return each table to an empty state.
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
I’d really like to know how to reset Django database.
Specifically, I accidentally deleted a table with this command,
$ python manage.py dbshell
$ DROP TABLE qaapp_question;
and everything messed up.
I followd a stackoverflow post and just deleted the table, and, haha, completely messed up. syncdb stuff is apparently deprecated so cannot use it.
I tried all the ways I googled and found, as followings:
$ python manage.py migrate qaapp zero
django.db.utils.OperationalError: no such table: qaapp_question
Not working.
$ manage.py migrate --fake <appname> zero
$ rm -rf migrations # I deleted the app migrations folder manually
$ manage.py makemigrations <appname>
$ manage.py migrate --fake <appname>
Not working.
$ python manage.py flush
Not working.
$ python manage.py reset_db
Reset successful.
Not working.
manually trying to delete the sqlite database file
-> resource busy or locked
Not working.
I wanted to restore the deleted table, but I don’t care anymore, just kindly tell me how to delete, initialize the database, then be able to rebuild the database with python manage.py makemigrations
and python manage.py migrate
.
Thanks for your help.
Simply deleting the SQlite database file should work. If you get "resource busy or locked" simply copy the entire project directory to somewhere else and try to delete it. Think of it, will you upload your database file altogether to heroku when deploying? I know it’s No, SQlite files should not be included to the git project and should be included in the .gitignore file. So, if the database file is nowhere to be found, Django will autogenerates a new database (reset) for you.
Simply run the command
python manage.py flush
And answer the following question with ‘yes’
This will IRREVERSIBLY DESTROY all data currently in the "qaapp" database,
and return each table to an empty state.
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes