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

前端 未结 18 1750
余生分开走
余生分开走 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
    

提交回复
热议问题