如何将一个表从DJango的SQLite3中删除?

时间:2022-08-07 00:16:14

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.

我做了一个模型,并运行了python管理。py syncdb。我认为这在db中创建了一个表。然后我意识到我做了一个不正确的列,所以我改变了它,运行了相同的命令,认为它会删除旧表,并添加一个新的表。

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

然后我去了python管理。py shell,并试图运行.objects.all(),但失败了,表示该列不存在。

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

我想清除旧表,然后再运行syncdb,但是我不知道怎么做。

6 个解决方案

#1


38  

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

在使用Django 1.4或以下时,另一种简单的方法是

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

这在Django 1.3中被弃用,在Django 1.5中不再可用

#2


68  

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

#3


31  

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.

没有一个答案显示了如何只删除应用程序中的一个表。dbshell命令将用户记录到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).

当您在shell中时,输入以下命令以查看数据库的结构。这将显示数据库中的所有表名(以及表中的列名)。

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:

通常,Django根据以下约定为表命名:“appname_modelname”。因此,实现您的目标的SQL查询将类似于以下内容:

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:

这应该是足够的,即使该表与其他表有关系。现在您可以执行以下操作,退出SQLITE shell:

.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.

如果再次运行syncdb, Django将根据模型重新构建表。通过这种方式,您可以更新数据库表,而不会丢失所有应用程序数据。如果您经常遇到这个问题,可以考虑使用South -一个django应用程序,它将为您迁移您的表。

#4


25  

get the DROP statements with

获取DROP语句

python manage.py sqlclear app_name

python管理。py sqlclear app_name

then try

那就试试

python manage.py dbshell

python管理。py dbshell

and execute the DROP statement

并执行DROP语句

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

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

#5


16  

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,

为了快速解决问题(如果您不关心丢失表/数据),请纠正模型。使用所需数据类型的py文件,删除迁移文件夹和db。SQLite3文件,

then re-run the following commands:

然后重新运行以下命令:

  1. python manage.py migrate
  2. python管理。py迁移
  3. python manage.py makemigrations
  4. python管理。py makemigrations
  5. python manage.py migrate
  6. python管理。py迁移
  7. python manage.py createsuperuser (to create an admin user/pswd to manage admin page)
  8. python管理。py createsuperuser(创建一个admin用户/pswd来管理admin页面)
  9. python manage.py runserver
  10. python管理。py runserver

#6


1  

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

在《被解救的姜戈》1.9中,我不得不走卡特·鲁索(Kat Russo)的路,但第二次迁移有点棘手。你必须运行

./manage.py migrate --run-syncdb

#1


38  

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

在使用Django 1.4或以下时,另一种简单的方法是

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

这在Django 1.3中被弃用,在Django 1.5中不再可用

#2


68  

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

#3


31  

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.

没有一个答案显示了如何只删除应用程序中的一个表。dbshell命令将用户记录到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).

当您在shell中时,输入以下命令以查看数据库的结构。这将显示数据库中的所有表名(以及表中的列名)。

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:

通常,Django根据以下约定为表命名:“appname_modelname”。因此,实现您的目标的SQL查询将类似于以下内容:

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:

这应该是足够的,即使该表与其他表有关系。现在您可以执行以下操作,退出SQLITE shell:

.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.

如果再次运行syncdb, Django将根据模型重新构建表。通过这种方式,您可以更新数据库表,而不会丢失所有应用程序数据。如果您经常遇到这个问题,可以考虑使用South -一个django应用程序,它将为您迁移您的表。

#4


25  

get the DROP statements with

获取DROP语句

python manage.py sqlclear app_name

python管理。py sqlclear app_name

then try

那就试试

python manage.py dbshell

python管理。py dbshell

and execute the DROP statement

并执行DROP语句

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

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

#5


16  

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,

为了快速解决问题(如果您不关心丢失表/数据),请纠正模型。使用所需数据类型的py文件,删除迁移文件夹和db。SQLite3文件,

then re-run the following commands:

然后重新运行以下命令:

  1. python manage.py migrate
  2. python管理。py迁移
  3. python manage.py makemigrations
  4. python管理。py makemigrations
  5. python manage.py migrate
  6. python管理。py迁移
  7. python manage.py createsuperuser (to create an admin user/pswd to manage admin page)
  8. python管理。py createsuperuser(创建一个admin用户/pswd来管理admin页面)
  9. python manage.py runserver
  10. python管理。py runserver

#6


1  

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

在《被解救的姜戈》1.9中,我不得不走卡特·鲁索(Kat Russo)的路,但第二次迁移有点棘手。你必须运行

./manage.py migrate --run-syncdb