How to remove, drop a table in a database with the django web framework ?

Active March 21, 2022    /    Viewed 68    /    Comments 0    /    Edit


Example of how to drop a table in a database with django:

Start django database shell

In the terminal go the the folder with the manage.py file and then enter the following command:

python manage.py dbshell

It is going to start a shell associated with the database type. For example with the default SQLite it will returns:

SQLite version 3.35.4 2021-04-02 15:20:15
Enter ".help" for usage hints.
sqlite>

SQLite database

Get all tables name

To get all table names just enter:

sqlite> .tables

it will returns for example:

sqlite> .tables
        auth_group                        
        auth_group_permissions               
        auth_permission                     
        auth_user                                    
        auth_user_groups             
        myapp_article

Get the number of rows in a table

To get the number of rows in a table

sqlite> SELECT COUNT(*) FROM myapp_article;
1563

Drop a given table

To drop a table:

DROP TABLE myapp_article;

References


Card image cap
profile-image
Daidalos

Hi, I am Ben.

I have developed this web site from scratch with Django to share with everyone my notes. If you have any ideas or suggestions to improve the site, let me know ! (you can contact me using the form in the welcome page). Thanks!



Did you find this content useful ?, If so, please consider donating a tip to the author(s). MoonBooks.org is visited by millions of people each year and it will help us to maintain our servers and create new contents.

Amount