简体   繁体   English

Django manage.py sqlclear省略了一些表

[英]Django manage.py sqlclear omit some tables

I wrote python script for dropping tables in all Django apps. 我编写了python脚本来删除所有Django应用程序中的表。 (using settings.INSTALLED_APP) (使用settings.INSTALLED_APP)

https://gist.github.com/1520683 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. 我的django项目在运行manage.py syncdb之后创建了41个表,但是我的脚本说只删除了40个表。 So, I examined the result of sqlall and result of sqlclear . 所以,我检查的结果sqlall和结果sqlclear And I revealed sqlclear omits one table that stores ManyToManyField relationship. 我透露sqlclear省略了一个存储ManyToManyField关系的表。

I knew that drop database is much simpler than the above script. 我知道drop database比上面的脚本简单得多。 But I confused why django admin or manage script omit some tables while running sql commands. 但我很困惑为什么django管理或管理脚本在运行sql命令时省略了一些表。


Below model creates common_userbook_purchasedBooks table while running syncdb , but not in sqlclear command. 下面的模型在运行syncdb创建common_userbook_purchasedBooks表,但在sqlclear命令中没有。

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 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 lqez,我猜这个问题与你的本地环境有关,因为对于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 当我运行(.env)testme$ ./manage.py sqlclear testapp输出看起来像

sqlite3 sqlite3的

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

postgresql_psycopg2 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 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 : 使用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']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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