如何删除Django中的数据库表? [英] How can i delete database table in django?

查看:50
本文介绍了如何删除Django中的数据库表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更改了模型并进行了移植.然后我又改变了一次模型,当尝试python manage.py migration时出现错误:

I changed my models and made migrations. Then i changed my models another one time and when try python manage.py migrate i get error:

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions, shop
Running migrations:
  Applying shop.0004_auto_20180128_1331...Traceback (most recent call last):
  File "/home/morilon/dj/intshop/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 83, in _execute
    return self.cursor.execute(sql)
  File "/home/morilon/dj/intshop/venv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 301, in execute
    return Database.Cursor.execute(self, query)
sqlite3.OperationalError: table "shop_brand" already exists

所以我的问题是-如何删除表"shop_brand" ???我已经尝试过 flush sqlflush ,但那只会从表中删除数据,而实际上并未删除表"shop_brand".

So my question is - how can i delete table "shop_brand"??? I already tried flush and sqlflush but that only delete data from table but not actually table "shop_brand".

我使用Django 2.0.1和python 3.6

I use django 2.0.1 and python 3.6

推荐答案

使用dbshel​​l命令

Use the dbshell command

python manage.py dbshell

然后,在外壳程序中,根据所使用的数据库,键入命令以显示表以标识要删除的表.

then while in the shell, depending on what database you are using, you type the command to show tables to identify the table you want to drop.

例如,对于sqlite,您将使用

For instance, for sqlite, you would use

.tables

仍然在外壳中,您可以使用SQL命令删除表

still in the shell, you can use SQL command to drop the table

DROP TABLE shop_brand;

这篇关于如何删除Django中的数据库表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆