【问题标题】:How can i delete database table in django?如何在 django 中删除数据库表?
【发布时间】:2018-01-28 16:35:35
【问题描述】:

我更改了模型并进行了迁移。然后我再次更改了我的模型,当尝试 python manage.py migrate 时出现错误:

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”??? 我已经尝试过flushsqlflush,但这只是从表中删除数据,而不是实际表“shop_brand”。

我使用 django 2.0.1 和 python 3.6

【问题讨论】:

  • 查看this
  • 只是删除所有文件是一种选择,但我想知道如何只删除数据库中的一个平板电脑。

标签: django database sql-delete


【解决方案1】:

使用 dbshel​​l 命令

python manage.py dbshell

然后在 shell 中,根据您使用的数据库,键入命令以显示表以识别要删除的表。

例如,对于 sqlite,您可以使用

.tables

还在shell中,可以使用SQL命令删除表

DROP TABLE shop_brand;

【讨论】:

  • 我试过了,但是 CommandError: 你似乎没有安装 'sqlite3' 程序或在你的路径上。
  • 或安装 sqlite3 sudo apt-get install sqlite3
  • 好的,我会试试的。我只是以为我只能用 Django 来处理它。
  • 为什么我仍然在管理站点中删除那些表:(?
猜你喜欢
  • 2020-11-21
  • 2016-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-28
  • 1970-01-01
  • 2020-02-02
  • 2018-11-09
相关资源
最近更新 更多