I have a number of models containing foreign keys and many to many (m2m) relationships in a Django project. When I try and use the sqlclear command from manage.py i receive the error:
ERROR: cannot drop table appname_tablename because other objects depend on it
Is there a way to have Django add CASCADE to the output so dependent tables can be dropped?
Why not use sed ?
>>> ./manage.py sqlclear testapp | sed 's/";/" CASCADE;/'
BEGIN;
DROP TABLE "testapp_person" CASCADE;
DROP TABLE "testapp_post" CASCADE;
DROP TABLE "testapp_userprofile" CASCADE;
DROP TABLE "testapp_school" CASCADE;
DROP TABLE "testapp_events" CASCADE;
DROP TABLE "testapp_uploadmodel" CASCADE;
COMMIT;
You can execute resulting DROP TABLE script several times until all tables will be dropped. Is this a problem for you?
EDITED
You can overwrite sql_delete in django.core.management.sql with:
...
if cursor and table_name_converter(model._meta.db_table) in table_names:
# Drop the table now
output.append('%s %s %s;' % (style.SQL_KEYWORD('DROP TABLE IF EXISTS'), #<-!
style.SQL_TABLE(qn(model._meta.db_table)),
style.SQL_KEYWORD('CASCADE'))) #<-!
...
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.