'What should happen to the data when disabling an app in django?

I have a django app "my_debug" that contains a model like this:

class MyDebugEntry(Model):
    user = ForeignKey(User, on_delete=CASCADE)
    data = TextField()

This app is only for debugging purposes, so I add it to INSTALLED_APPS when I need it and remove it afterwards. This works fine for the most part.

However, when I try to remove a user I get the following error message:

IntegrityError at /some/path/
update or delete on table "auth_users" violates foreign key constraint "mydebug_mydebugentry_user_id_d738bc03_fk_users_" on table "mydebug_mydebugentry"
DETAIL:  Key (id)=(5) is still referenced from table "mydebug_mydebugentry".

This is because on_delete=CASCADE is implemented in python, not in the database itself. So when the "my_debug" app is disabled, the on_deleted behavior is disabled along with it.

So what is the proper way to do this? Should I drop all of the app's tables when removing it from INSTALLED_APPS?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source