How do I drop a table from SQLite3 in DJango?

Viewed 74096

I made a model, and ran python manage.py syncdb. I think that created a table in the db. Then I realized that I had made a column incorrectly, so I changed it, and ran the same command, thinking that it would drop the old table, and add a new one.

Then I went to python manage.py shell, and tried to run .objects.all(), and it failed, saying that column doesn't exist.

I want to clear out the old table, and then run syncdb again, but I can't figure out how to do that.

8 Answers

Another simple way to do this while using Django 1.4 or below, would be

python manage.py reset app_name

which drops and re-creates the tables used by the models of this app.

This was deprecated in Django 1.3 and is no longer available from Django 1.5

In Django 2.1.7, I've opened the db.sqlite3 file in SQLite browser (there is also a Python package on Pypi) and deleted the table using command

DROP TABLE appname_tablename;

and then

DELETE FROM django_migrations WHERE App='appname';

Then run again

python manage.py makemigrations appname
python manage.py migrate appname

Might be worth expanding on a few answers here:

So, you can get access to the dbshell with the following command:

python manage.py dbshell

It is then preferential to use the following:

DROP TABLE appname_tablename CASCADE;

To drop any related tables, effectively mirroring the "on_delete=CASCADE" direction on a field.

Related