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

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

    If you are using psql and have django-more 2.0.0 installed, you can do

    manage.py reset_schema

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

    There's an even simpler answer if you want to delete ALL your tables. You just go to your folder containing the database (which may be called mydatabase.db) and right-click the .db file and push "delete." Old fashioned way, sure-fire to work.

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

    It is better to use ./manage.py sqlflush | ./manage.py dbshell because sqlclear requires app to flush.

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

    If you want to completely wipe the database and resync it in the same go you need something like the following. I also combine adding test data in this command:

    #!/usr/bin/env python
    
    import os
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "main.settings") # Replace with your app name.
    
    from django.db import connection
    from django.core.management import call_command
    from django.conf import settings
    # If you're using postgres you can't use django's sql stuff for some reason that I
    # can't remember. It has to do with that autocommit thing I think.
    # import psychodb2 as db
    
    def recreateDb():
        print("Wiping database")
        dbinfo = settings.DATABASES['default']
    
        # Postgres version
        #conn = db.connect(host=dbinfo['HOST'], user=dbinfo['USER'],
        #                 password=dbinfo['PASSWORD'], port=int(dbinfo['PORT'] or 5432))
        #conn.autocommit = True
        #cursor = conn.cursor()
        #cursor.execute("DROP DATABASE " + dbinfo['NAME'])
        #cursor.execute("CREATE DATABASE " + dbinfo['NAME'] + " WITH ENCODING 'UTF8'") # Default is UTF8, but can be changed so lets be sure.
    
        # Mysql version:
        print("Dropping and creating database " + dbinfo['NAME'])
        cursor = connection.cursor()
        cursor.execute("DROP DATABASE " + dbinfo["NAME"] + "; CREATE DATABASE " + dbinfo["NAME"] + "; USE " + dbinfo["NAME"] + ";")
        print("Done")
    
    
    if __name__ == "__main__":
        recreateDb();
        print("Syncing DB")
        call_command('syncdb', interactive=False)
        print("Adding test data")
        addTestData() # ...
    

    It would be nice to be able to do cursor.execute(call_command('sqlclear', 'main')) but call_command prints the SQL to stdout rather than returning it as a string, and I can't work out the sql_delete code...

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

    python manage.py migrate <app> zero

    sqlclear was removed from 1.9.

    Release notes mention that it is due to the introduction of migrations: https://docs.djangoproject.com/en/1.9/releases/1.9/

    Unfortunately I could not find a method that works on all apps at once, nor a built-in way to list all installed apps from the admin: How to list all installed apps with manage.py in Django?

    Related: How to reset migrations in Django 1.7?

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

    Using Python to make a flushproject command, you use :

    from django.db import connection
    cursor = connection.cursor()
    cursor.execute(“DROP DATABASE %s;”, [connection.settings_dict['NAME']])
    cursor.execute(“CREATE DATABASE %s;”, [connection.settings_dict['NAME']])
    
    0 讨论(0)
提交回复
热议问题