Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

django drop all tables

The following is a function to drop all the public tables from the
database using: python manage.py dropalltables

1 - In the app that contains the models, create:
  management/commands/dropalltables.py

The tree structure should look like this:
  my_app
     |---management
             |---commands
                     |---dropalltables.py

        
2 - in dropalltables.py write:
  
from django.core.management.base import BaseCommand, CommandError
from django.db import connection


class Command(BaseCommand):
    help = "Drop all the tables from the database"

    def handle(self, *args, **options):
        try:
            print("Do you really want to DROP (delete) ALL THE PUBLIC TABLES from the database?")
            choice = input("type the following sentence to confirm: yes, drop all! ")
            if choice.lower() == "yes, drop all!":
                cursor = connection.cursor()
                cursor.execute("SELECT table_name FROM INFORMATION_SCHEMA.tables where table_schema = 'public';")
                parts = ('DROP TABLE IF EXISTS %s CASCADE;' % table for (table,) in cursor.fetchall())
                sql = '
'.join(parts) + '
'
                connection.cursor().execute(sql)
                print("Done! There are no more public tables in the database.")
            else:
                print("You have chosen not to remove the tables.")

        except Exception as e:
            raise CommandError(f"Error: {e}")

3 - In the terminal type:
  python manage.py dropalltables
Comment

PREVIOUS NEXT
Code Example
Python :: python remove punctuation from text file 
Python :: python plot multiple lines in same figure 
Python :: streamlit python install 
Python :: append vs insert python 
Python :: how to add two matrix using function in python 
Python :: python read lines 
Python :: get mode dataframe 
Python :: try except keyerror 
Python :: python iterate through string in reverse 
Python :: number of words in a string python 
Python :: python summary() 
Python :: the following packages have unmet dependencies python3-tornado 
Python :: plt.imread python 
Python :: how to check if a input is an integer python 
Python :: matplotlib measure the width of text 
Python :: make a script run itself again python 
Python :: plot background color matplotlib 
Python :: how to get bot voice channel discord.py 
Python :: how to do disconnect command on member in discord python 
Python :: check dir exist python 
Python :: get root path python 
Python :: shutdown flask server with request 
Python :: python subprocess print stdout while process running 
Python :: user input of int type in python 
Python :: how to print answer 2 decimal places in python 3 
Python :: how to write a while statement in python 
Python :: when button is clicked tkinter python 
Python :: date strftime python 
Python :: str to tuple of float 
Python :: crear una clase en python 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =