From b23c63bcfeb870c46889fbf1d36874017c358c90 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Tue, 26 Jul 2005 22:38:54 +0000 Subject: [PATCH] Fixed #206 -- 'django-admin sqlclear' now deletes from admin log to prevent referential integrity violations git-svn-id: http://code.djangoproject.com/svn/django/trunk@323 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/management.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/django/core/management.py b/django/core/management.py index a2953205b2..435ba54449 100644 --- a/django/core/management.py +++ b/django/core/management.py @@ -132,9 +132,20 @@ def get_sql_delete(mod): pass else: output.append("DROP TABLE %s;" % f.get_m2m_db_table(opts)) - output.append("DELETE FROM packages WHERE label = '%s';" % mod._MODELS[0]._meta.app_label) - output.append("DELETE FROM auth_permissions WHERE package = '%s';" % mod._MODELS[0]._meta.app_label) - output.append("DELETE FROM content_types WHERE package = '%s';" % mod._MODELS[0]._meta.app_label) + + app_label = mod._MODELS[0]._meta.app_label + + # Delete from packages, auth_permissions, content_types. + output.append("DELETE FROM packages WHERE label = '%s';" % app_label) + output.append("DELETE FROM auth_permissions WHERE package = '%s';" % app_label) + output.append("DELETE FROM content_types WHERE package = '%s';" % app_label) + + # Delete from the admin log. + if cursor is not None: + cursor.execute("SELECT id FROM content_types WHERE package = %s", [app_label]) + for row in cursor.fetchall(): + output.append("DELETE FROM auth_admin_log WHERE content_type_id = %s;" % row[0]) + return output[::-1] # Reverse it, to deal with table dependencies. get_sql_delete.help_doc = "Prints the DROP TABLE SQL statements for the given app(s)." get_sql_delete.args = APP_ARGS