How to drop all tables from the database with manage.py CLI in Django?

前端 未结 18 1579
余生分开走
余生分开走 2020-11-29 16:54

How can I drop all tables from a database using manage.py and command line? Is there any way to do that executing manage.py with appropriate parameters so I can execute it f

相关标签:
18条回答
  • 2020-11-29 17:34

    Here's a shell script I ended up piecing together to deal with this issue. Hope it saves someone some time.

    #!/bin/sh
    
    drop() {
        echo "Droping all tables prefixed with $1_."
        echo
        echo "show tables" | ./manage.py dbshell |
        egrep "^$1_" | xargs -I "@@" echo "DROP TABLE @@;" |
        ./manage.py dbshell
        echo "Tables dropped."
        echo
    }
    
    cancel() {
        echo "Cancelling Table Drop."
        echo
    }
    
    if [ -z "$1" ]; then
        echo "Please specify a table prefix to drop."
    else
        echo "Drop all tables with $1_ prefix?"
        select choice in drop cancel;do
            $choice $1
            break
        done
    fi
    
    0 讨论(0)
  • 2020-11-29 17:34

    Drops all tables and recreates them:

    python manage.py sqlclear app1 app2 appN | sed -n "2,$p" | sed -n "$ !p" | sed "s/";/" CASCADE;/" | sed -e "1s/^/BEGIN;/" -e "$s/$/COMMIT;/" | python manage.py dbshell
    python manage.py syncdb
    

    Explanation:

    manage.py sqlclear - "prints the DROP TABLE SQL statements for the given app name(s)"

    sed -n "2,$p" - grabs all lines except first line

    sed -n "$ !p" - grabs all lines except last line

    sed "s/";/" CASCADE;/" - replaces all semicolons (;) with (CASCADE;)

    sed -e "1s/^/BEGIN;/" -e "$s/$/COMMIT;/" - inserts (BEGIN;) as first text, inserts (COMMIT;) as last text

    manage.py dbshell - "Runs the command-line client for the database engine specified in your ENGINE setting, with the connection parameters specified in your USER, PASSWORD, etc., settings"

    manage.py syncdb - "Creates the database tables for all apps in INSTALLED_APPS whose tables have not already been created"

    Dependencies:

    • sed, to use on Windows, I installed UnxUtils: (Download) (Installation Instructions)

    Credits:

    @Manoj Govindan and @Mike DeSimone for sqlclear piped to dbshell

    @jpic for 'sed "s/";/" CASCADE;/"'

    0 讨论(0)
  • 2020-11-29 17:36

    simple(?) way to do it from python (on mysql):

    from django.db import connection
    
    cursor = connection.cursor()
    cursor.execute('show tables;')
    parts = ('DROP TABLE IF EXISTS %s;' % table for (table,) in cursor.fetchall())
    sql = 'SET FOREIGN_KEY_CHECKS = 0;\n' + '\n'.join(parts) + 'SET FOREIGN_KEY_CHECKS = 1;\n'
    connection.cursor().execute(sql)
    
    0 讨论(0)
  • 2020-11-29 17:37

    There's no native Django management command to drop all tables. Both sqlclear and reset require an app name.

    However, you can install Django Extensions which gives you manage.py reset_db, which does exactly what you want (and gives you access to many more useful management commands).

    0 讨论(0)
  • 2020-11-29 17:38

    I would recommend you to install django-extensions and use python manage.py reset_db command. It does exactly what you want.

    0 讨论(0)
  • 2020-11-29 17:39

    This answer is for postgresql DB:

    Run: echo 'drop owned by some_user' | ./manage.py dbshell

    NOTE: some_user is the name of the user you use to access the database, see settings.py file:

    default_database = {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'somedbname',
        'USER': 'some_user',
        'PASSWORD': 'somepass',
        'HOST': 'postgresql',
        'PORT': '',
    }
    
    0 讨论(0)
提交回复
热议问题