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

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

    If you're using the South package to handle database migrations (highly recommended), then you could just use the ./manage.py migrate appname zero command.

    Otherwise, I'd recommend the ./manage.py dbshell command, piping in SQL commands on standard input.

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

    use "python manage.py sqlflush" command in windows 10 for others type manage.py

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

    Here's an example Makefile to do some nice things with multiple settings files:

    test:
        python manage.py test --settings=my_project.test
    
    db_drop:
        echo 'DROP DATABASE my_project_development;' | ./manage.py dbshell
        echo 'DROP DATABASE my_project_test;' | ./manage.py dbshell
    
    db_create:
        echo 'CREATE DATABASE my_project_development;' | ./manage.py dbshell
        echo 'CREATE DATABASE my_project_test;' | ./manage.py dbshell
    
    db_migrate:
        python manage.py migrate --settings=my_project.base
        python manage.py migrate --settings=my_project.test
    
    db_reset: db_drop db_create db_migrate
    
    .PHONY: test db_drop db_create db_migrate db_reset
    

    Then you can do things like: $ make db_reset

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

    Here's a south migration version of @peter-g's answer. I often fiddle with raw sql, so this comes in handy as 0001_initial.py for any befuddled apps. It will only work on DBs that support SHOW TABLES (like mysql). Substitute something like SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'; if you use PostgreSQL. Also, I often do this exact same thing for both the forwards and backwards migrations.

    from south.db import db
    from south.v2 import SchemaMigration
    from django.db.utils import DatabaseError
    from os import path
    from logging import getLogger
    logger = getLogger(__name__)
    
    
    class Migration(SchemaMigration):
    
        def forwards(self, orm):
    
            app_name = path.basename(path.split(path.split(path.abspath(__file__))[0])[0])
            table_tuples = db.execute(r"SHOW TABLES;")
    
            for tt in table_tuples:
                table = tt[0]
                if not table.startswith(app_name + '_'):
                    continue
                try:
                    logger.warn('Deleting db table %s ...' % table)
                    db.delete_table(table)
                except DatabaseError:
                    from traceback import format_exc
                    logger.error("Error running %s: \n %s" % (repr(self.forwards), format_exc()))
    

    Coworker/cocoders would kill me if they knew I did this, though.

    0 讨论(0)
  • 2020-11-29 18:00

    As far as I know there is no management command to drop all tables. If you don't mind hacking Python you can write your own custom command to do that. You may find the sqlclear option interesting. Documentation says that ./manage.py sqlclear Prints the DROP TABLE SQL statements for the given app name(s).

    Update: Shamelessly appropriating @Mike DeSimone's comment below this answer to give a complete answer.

    ./manage.py sqlclear | ./manage.py dbshell
    

    As of django 1.9 it's now ./manage.py sqlflush

    0 讨论(0)
  • 2020-11-29 18:00

    The command ./manage.py sqlclear or ./manage.py sqlflush seems to clear the table and not delete them, however if you want to delete the complete database try this : manage.py flush.

    Warning: this will delete your database completely and you will lose all your data, so if that not important go ahead and try it.

    0 讨论(0)
提交回复
热议问题