Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i delete database table in django?

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

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

I use django 2.0.1 and python 3.6

like image 326
Loctarogar Avatar asked Dec 07 '22 15:12

Loctarogar


1 Answers

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.

For instance, for sqlite, you would use

.tables

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

DROP TABLE shop_brand;
like image 87
Kevin L. Avatar answered Jan 08 '23 10:01

Kevin L.