简体   繁体   中英

Django manage.py sqlclear omit some tables

I wrote python script for dropping tables in all Django apps. (using settings.INSTALLED_APP)

https://gist.github.com/1520683

My django project creates 41 tables after running manage.py syncdb , but my script says only 40 tables will be dropped. So, I examined the result of sqlall and result of sqlclear . And I revealed sqlclear omits one table that stores ManyToManyField relationship.

I knew that drop database is much simpler than the above script. But I confused why django admin or manage script omit some tables while running sql commands.


Below model creates common_userbook_purchasedBooks table while running syncdb , but not in sqlclear command.

class UserBook(models.Model):                       
    user = models.OneToOneField(User)
    purchasedBooks = models.ManyToManyField(Book)

Added) So, I'm using an alternative approach for this. https://gist.github.com/1520810

lqez, I gues this issue related to you local environment, because for Django 1.3.1, Python 2.7.2

for models

from django.contrib.auth.models import User
from django.db import models

class Book(models.Model):
    name = models.CharField(max_length=10)

class UserBook(models.Model):
    user = models.OneToOneField(User)
    purchasedBooks = models.ManyToManyField(Book)

when I run (.env)testme$ ./manage.py sqlclear testapp output looks like

sqlite3

BEGIN;
DROP TABLE "testapp_userbook";
DROP TABLE "testapp_userbook_purchasedBooks";
DROP TABLE "testapp_book";
COMMIT;

postgresql_psycopg2

BEGIN;
ALTER TABLE "testapp_userbook_purchasedBooks" DROP CONSTRAINT "userbook_id_refs_id_8bda4b0";
DROP TABLE "testapp_userbook";
DROP TABLE "testapp_userbook_purchasedBooks";
DROP TABLE "testapp_book";
COMMIT;

mysql

BEGIN;
ALTER TABLE `testapp_userbook_purchasedBooks` DROP FOREIGN KEY `userbook_id_refs_id_8bda4b0`;
DROP TABLE `testapp_userbook`;
DROP TABLE `testapp_userbook_purchasedBooks`;
DROP TABLE `testapp_book`;
COMMIT;

Also your script can be a little bit improved using introspection :

from django.db import connection
cursor = connection.cursor()
connection.introspection.get_table_list(cursor)

[u'auth_group', u'auth_group_permissions', u'auth_message', u'auth_permission', u'auth_user', u'auth_user_groups', u'auth_user_user_permissions', u'django_content_type', u'django_session', u'django_site', u'testapp_book', u'testapp_userbook', u'testapp_userbook_purchasedBooks']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM