Reputation: 1392
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.
Upvotes: 1
Views: 8422
Reputation: 31
I was in your situation after cloning a beanstalk environment with the RDS loading a bad snapshot that seemed incompatible with my migrations. All the same commands didn't work. What worked for me in the end was:
python manage.py migrate app_name zero
Then I was able to migrate like normal and the errors went away.
Upvotes: 3
Reputation: 4287
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
Upvotes: 3
Reputation: 302
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.
Upvotes: 0