How do I drop a table from SQLite3 in DJango?

2019-01-12 19:12发布

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.

6条回答
看我几分像从前
2楼-- · 2019-01-12 19:38

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

查看更多
淡お忘
3楼-- · 2019-01-12 19:42

None of the answers shows how to delete just one table in an app. It's not too difficult. The dbshell command logs the user into the sqlite3 shell.

python manage.py dbshell

When you are in the shell, type the following command to see the structure of your database. This will show you all the table names in the database (and also the column names within tables).

SELECT * FROM sqlite_master WHERE type='table';

In general, Django names tables according to the following convention: "appname_modelname". Therefore, SQL query that accomplishes your goal will look similar to the following:

DROP TABLE appname_modelname;

This should be sufficient, even if the table had relationships with other tables. Now you can log out of SQLITE shell by executing:

.exit

If you run syncdb again, Django will rebuild the table according to your model. This way, you can update your database tables without losing all of the app data. If you are running into this problem a lot, consider using South - a django app that will migrate your tables for you.

查看更多
▲ chillily
4楼-- · 2019-01-12 19:45

get the DROP statements with

python manage.py sqlclear app_name

then try

python manage.py dbshell

and execute the DROP statement

check out http://docs.djangoproject.com/en/dev/ref/django-admin/

查看更多
混吃等死
5楼-- · 2019-01-12 19:45

I had the same problem.

For a quick resolve (if you don't care about losing your tables/data), correct your models.py file with the desired data types, delete the Migration folder and db.SQLite3 file,

then re-run the following commands:

  1. python manage.py migrate
  2. python manage.py makemigrations
  3. python manage.py migrate
  4. python manage.py createsuperuser (to create an admin user/pswd to manage admin page)
  5. python manage.py runserver
查看更多
我想做一个坏孩纸
6楼-- · 2019-01-12 19:48

In Django 1.9 I had to do Kat Russo's steps, but the second migration was a little bit tricky. You have to run

./manage.py migrate --run-syncdb
查看更多
Bombasti
7楼-- · 2019-01-12 19:51

to clear out an application is as simple as writing:

./manage.py sqlclear app_name | ./manage.py dbshell 

then in order to rebuild your tables just type:

./manage.py syncdb
查看更多
登录 后发表回答